package com.wing.wcabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.controller.websocket.CabinetOpenServer;
import com.wing.cabinet.model.entity.*;
import com.wing.cabinet.model.request.StoresRequisitionProductForm;
import com.wing.cabinet.model.request.UseInventoryInoutDetailForm;
import com.wing.cabinet.model.response.CabinetCargoLaneVO;
import com.wing.cabinet.service.CabinetCargoLaneService;
import com.wing.cabinet.service.CabinetService;
import com.wing.cabinet.service.StoresRequisitionService;
import com.wing.common.constant.GlobalConstants;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.order.model.entity.OrderReceiveLoad;
import com.wing.order.model.entity.OrderReceiveLoadDetail;
import com.wing.order.model.request.OrderReceiveLoadDetailForm;
import com.wing.order.model.request.OrderReceiveLoadForm;
import com.wing.order.service.DynamicService;
import com.wing.order.service.OrderReceiveLoadDetailService;
import com.wing.order.service.OrderReceiveLoadService;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.entity.ProductWeightRecord;
import com.wing.product.model.response.ProductDetailVO;
import com.wing.product.service.ProductDetailService;
import com.wing.product.service.ProductWeightRecordService;
import com.wing.setting.model.entity.SystemRuleInfo;
import com.wing.setting.service.SystemRuleInfoService;
import com.wing.wcabinet.constant.CLICKConstants;
import com.wing.wcabinet.dao.WcabCellInventoryInoutDetailDao;
import com.wing.wcabinet.model.entity.*;
import com.wing.wcabinet.model.request.*;
import com.wing.wcabinet.model.response.WcabCellInventoryInoutDetailVO;
import com.wing.wcabinet.model.response.WcabCellInventorySpaceVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 售刀柜上货(上货主表无展示)/领取明细-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class WcabCellInventoryInoutDetailServiceImpl extends ServiceImpl<WcabCellInventoryInoutDetailDao, WcabCellInventoryInoutDetail> implements WcabCellInventoryInoutDetailService {

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private OrderReceiveLoadService orderReceiveLoadService;

    @Autowired
    private OrderReceiveLoadDetailService orderReceiveLoadDetailService;

    @Autowired
    private WcabCellInventoryInoutService inventoryInoutService;

    @Autowired
    private WcabCellInventorySpaceService inventorySpaceService;

    @Autowired
    private WcabCellSingleInventoryProductService storageSingleInventoryProductService;

    @Autowired
    private WcabCellInventoryProductService inventoryProductService;

    @Autowired
    private CabinetCargoLaneService laneService;

    @Autowired
    private SystemRuleInfoService systemRuleInfoService;

    @Autowired
    private WcabUseInventoryInoutDetailService useInventoryInoutDetailService;

    @Autowired
    private ProductDetailService productDetailService;

    @Autowired
    private WcabCellInventoryUserService cellInventoryUserService;

    @Autowired
    private WcabCellInventoryCheckService storageInventoryCheckService;

    @Autowired
    private WcabCellInventoryCheckDetailService wcabCellInventoryCheckDetailService;

    @Autowired
    private WcabCellInventoryCheckInoutService wcabCellInventoryCheckInoutService;

    @Autowired
    private WcabCellLoadService wcabCellLoadService;

    @Autowired
    private WcabCellLoadDetailService wcabCellLoadDetailService;

    @Autowired
    private WcabCheckedInventoryInoutDetailService checkedInventoryInoutDetailService;


    @Autowired
    private WcabCheckedResultDetailService checkedResultDetailService;


    @Autowired
    private CabinetService cabinetService;

    @Autowired
    private CabinetCargoLaneService cargoLaneService;

    @Autowired
    private WcabCellInventoryUserService inventoryUserService;

    @Autowired
    private WcabCheckedResultService checkedResultService;

    @Autowired
    private StoresRequisitionService storesRequisitionService;

    @Autowired
    private ProductWeightRecordService productWeightRecordService;

    @Autowired
    private WcabCellInventoryOutSeqService cellInventoryOutSeqService;

    @Autowired
    private CLICKMsgService clickMsgService;

    @Autowired
    private WauthorityManageService authorityManageService;
    @Autowired
    private WauthorityMaterielService authorityMaterielService;
    @Autowired
    private WauthorityMemberService authorityMemberService;

    private MathContext mathContext = new MathContext(20, RoundingMode.HALF_UP);

    private ExecutorService executorService= Executors.newFixedThreadPool(10);



    @Override
    public int add(WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm) {

        //校验

        WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail =new WcabCellInventoryInoutDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(WcabCellInventoryInoutDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(wcabCellInventoryInoutDetailForm, wcabCellInventoryInoutDetail, copyOptions);

        return baseMapper.insert(wcabCellInventoryInoutDetail);
    }

    @Override
    public List<WcabCellInventoryInoutDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<WcabCellInventoryInoutDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<WcabCellInventoryInoutDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<WcabCellInventoryInoutDetailVO> pageList(Page page, Map<String, Object> params) {
        List<WcabCellInventoryInoutDetailVO> list = baseMapper.list(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WcabCellInventoryInoutDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public void inventoryIn4Batch(OrderReceiveLoadForm orderReceiveLoadForm) {
        Long enterpriseId= JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();

        if (CollectionUtil.isEmpty(orderReceiveLoadForm.getDetailList())){
            throw new BusinessException("缺少上货明细参数");
        }

        WcabCellInventoryInoutForm wcabCellInventoryInoutForm=new WcabCellInventoryInoutForm();
//        wcabCellInventoryInoutForm.setReceiveLoadId(orderReceiveLoad.getId());
        List<WcabCellInventoryInoutDetailForm> detailList=new ArrayList<>();
//        CellInventoryInoutDetailForm cellInventoryInoutDetailForm=new CellInventoryInoutDetailForm();
//        cellInventoryInoutDetailForm.setCabCode(orderReceiveLoadForm.getCabCode());
//        cellInventoryInoutDetailForm.setSkuCode(orderReceiveLoadForm.getSkuCode());
//        cellInventoryInoutDetailForm.setSpaceCode(orderReceiveLoadForm.getSpaceCode());
//        cellInventoryInoutDetailForm.setQuantity(orderReceiveLoadForm.getQuantity());
//
//        detailList.add(cellInventoryInoutDetailForm);
        for(OrderReceiveLoadDetailForm orderReceiveLoadDetailForm:orderReceiveLoadForm.getDetailList()){
            WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=new WcabCellInventoryInoutDetailForm();
            wcabCellInventoryInoutDetailForm.setCabCode(orderReceiveLoadDetailForm.getCabCode());
            wcabCellInventoryInoutDetailForm.setSkuCode(orderReceiveLoadDetailForm.getSkuCode());
            wcabCellInventoryInoutDetailForm.setSpaceCode(orderReceiveLoadDetailForm.getSpaceCode());
            wcabCellInventoryInoutDetailForm.setQuantity(orderReceiveLoadDetailForm.getQuantity());
            wcabCellInventoryInoutDetailForm.setWeight(orderReceiveLoadDetailForm.getWeight());

            detailList.add(wcabCellInventoryInoutDetailForm);
        }
        wcabCellInventoryInoutForm.setDetailList(detailList);

        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        BeanUtils.copyProperties(wcabCellInventoryInoutForm,wcabCellInventoryInout);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("INS-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(creator);
        wcabCellInventoryInout.setCreatorName(creatorName);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
        wcabCellInventoryInout.setOperationType(orderReceiveLoadForm.getOperationType());
        wcabCellInventoryInoutForm.setCreator(creator);
        wcabCellInventoryInoutForm.setCreatorName(creatorName);
//        saveReceiveLoad(wcabCellInventoryInoutForm);

        OrderReceiveLoad orderReceiveLoad=new OrderReceiveLoad();
        BeanUtils.copyProperties(orderReceiveLoadForm,orderReceiveLoad);
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        code=dynamicService.getDayNextCode("ord_order_receive_load");
//        String day=simpleDateFormat.format(new Date());
        orderReceiveLoad.setCode("RLS-"+day+"-"+code);
        orderReceiveLoad.setDay(Integer.valueOf(day));
        orderReceiveLoad.setNo(Integer.valueOf(code));
        orderReceiveLoad.setCreator(JwtUtils.getLoginUserId());
        orderReceiveLoad.setCreatorName(JwtUtils.getLoginUsername());

        orderReceiveLoadService.save(orderReceiveLoad);
        saveReceiveLoadDetail(orderReceiveLoadForm,orderReceiveLoad.getId());
        wcabCellInventoryInout.setReceiveLoadId(orderReceiveLoad.getId());

        inventoryInoutService.save(wcabCellInventoryInout);
        saveDetail(wcabCellInventoryInoutForm.getDetailList(),wcabCellInventoryInout.getId(),CellInventoryInout.Type.in.name(),wcabCellInventoryInout.getBusinessType(),false);
        //异步同步数据
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
//        sendMsg(wcabCellInventoryInout,wcabCellInventoryInoutForm);
    }

    @Override
    @Transactional
    public void inventoryIn4Batch(WcabCellLoadForm wcabCellLoadForm) {
        Long enterpriseId= JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String departmentName=JwtUtils.getDepartmentName();
        String roleName=JwtUtils.getRoleName();

        if (CollectionUtil.isEmpty(wcabCellLoadForm.getDetailList())){
            throw new BusinessException("缺少上货明细参数");
        }

        WcabCellInventoryInoutForm wcabCellInventoryInoutForm=new WcabCellInventoryInoutForm();
//        wcabCellInventoryInoutForm.setReceiveLoadId(wcabCellLoad.getId());
        List<WcabCellInventoryInoutDetailForm> detailList=new ArrayList<>();
//        CellInventoryInoutDetailForm cellInventoryInoutDetailForm=new CellInventoryInoutDetailForm();
//        cellInventoryInoutDetailForm.setCabCode(wcabCellLoadForm.getCabCode());
//        cellInventoryInoutDetailForm.setSkuCode(wcabCellLoadForm.getSkuCode());
//        cellInventoryInoutDetailForm.setSpaceCode(wcabCellLoadForm.getSpaceCode());
//        cellInventoryInoutDetailForm.setQuantity(wcabCellLoadForm.getQuantity());
//
//        detailList.add(cellInventoryInoutDetailForm);
        for(WcabCellLoadDetailForm wcabCellLoadDetailForm:wcabCellLoadForm.getDetailList()){
            WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=new WcabCellInventoryInoutDetailForm();
            wcabCellInventoryInoutDetailForm.setCabCode(wcabCellLoadDetailForm.getCabCode());
            wcabCellInventoryInoutDetailForm.setSkuCode(wcabCellLoadDetailForm.getSkuCode());
            wcabCellInventoryInoutDetailForm.setSpaceCode(wcabCellLoadDetailForm.getSpaceCode());
            wcabCellInventoryInoutDetailForm.setQuantity(wcabCellLoadDetailForm.getQuantity());
            wcabCellInventoryInoutDetailForm.setWeight(wcabCellLoadDetailForm.getWeight());

            detailList.add(wcabCellInventoryInoutDetailForm);
        }
        wcabCellInventoryInoutForm.setDetailList(detailList);

        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        BeanUtils.copyProperties(wcabCellInventoryInoutForm,wcabCellInventoryInout);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("INS-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(creator);
        wcabCellInventoryInout.setCreatorName(creatorName);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
        wcabCellInventoryInout.setOperationType(WcabCellInventoryInout.OperationType.jsonCreator(wcabCellLoadForm.getOperationType().name()));
        wcabCellInventoryInoutForm.setCreator(creator);
        wcabCellInventoryInoutForm.setCreatorName(creatorName);
//        saveReceiveLoad(wcabCellInventoryInoutForm);

        WcabCellLoad wcabCellLoad=new WcabCellLoad();
        BeanUtils.copyProperties(wcabCellLoadForm,wcabCellLoad);
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        code=dynamicService.getDayNextCode("wcab_cell_load");
//        String day=simpleDateFormat.format(new Date());
        wcabCellLoad.setCode("LO-"+day+"-"+code);
        wcabCellLoad.setDay(Integer.valueOf(day));
        wcabCellLoad.setNo(Integer.valueOf(code));
        wcabCellLoad.setCreator(JwtUtils.getLoginUserId());
        wcabCellLoad.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellLoad.setEnterpriseCode(enterpriseCode);
        wcabCellLoad.setDepartmentName(departmentName);
        wcabCellLoad.setRoleName(roleName);

        wcabCellLoadService.save(wcabCellLoad);
        wcabCellLoadForm.setId(wcabCellLoad.getId());
        saveReceiveLoadDetail(wcabCellLoadForm,wcabCellLoad.getId());
        wcabCellInventoryInout.setReceiveLoadId(wcabCellLoad.getId());
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
        wcabCellInventoryInout.setDepartmentName(departmentName);
        wcabCellInventoryInout.setRoleName(roleName);

        inventoryInoutService.save(wcabCellInventoryInout);
        wcabCellLoadForm.setBusinessId(wcabCellInventoryInout.getId());
        saveDetail(wcabCellInventoryInoutForm.getDetailList(),wcabCellInventoryInout.getId(),CellInventoryInout.Type.in.name(),wcabCellInventoryInout.getBusinessType(),false);
        //异步同步数据
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
//        sendMsg(wcabCellInventoryInout,wcabCellInventoryInoutForm);

    }





    @Override
    @Transactional
    public void inventoryOutIn(WcabCellInventoryInoutForm wcabCellInventoryInoutForm) {
        List<WcabCellInventoryInoutForm> list=this.getWcabCellInventoryInoutFormList(wcabCellInventoryInoutForm);
        for (WcabCellInventoryInoutForm orderReceiveLoadForm:list){
            this.inventoryOut(orderReceiveLoadForm);
            for (WcabCellInventoryInoutDetailForm detailForm:orderReceiveLoadForm.getDetailList()){
                detailForm.setCabIdSource(detailForm.getCabId());
                detailForm.setCabCodeSource(detailForm.getCabCodeSource());
                detailForm.setSpaceIdSource(detailForm.getSpaceId());
                detailForm.setSpaceCodeSource(detailForm.getSpaceCode());

                detailForm.setCabId(detailForm.getCabIdTarget());
                detailForm.setCabCode(detailForm.getCabCodeTarget());
                detailForm.setSpaceId(detailForm.getSpaceIdTarget());
                detailForm.setSpaceCode(detailForm.getSpaceCodeTarget());
            }
            this.inventoryIn(orderReceiveLoadForm);
        }

    }

    private List<WcabCellInventoryInoutForm> getWcabCellInventoryInoutFormList(WcabCellInventoryInoutForm wcabCellInventoryInoutForm){
        List<WcabCellInventoryInoutForm> resultList=new ArrayList<>();
        Map<WcabCellInventoryInoutDetail.BusinessType,WcabCellInventoryInoutForm> resultMap=new HashMap<>();
        for (WcabCellInventoryInoutDetailForm detailForm:wcabCellInventoryInoutForm.getDetailList()){
            WcabCellInventoryInoutForm temp=resultMap.get(detailForm.getBusinessType());
            if (temp==null){
                temp=new WcabCellInventoryInoutForm();
                temp.setBusinessType(WcabCellInventoryInout.BusinessType.jsonCreator(detailForm.getBusinessType().name()));
                temp.setAllocationId(wcabCellInventoryInoutForm.getAllocationId());
                List<WcabCellInventoryInoutDetailForm> list=new ArrayList<>();
                list.add(detailForm);
                temp.setDetailList(list);
                resultMap.put(detailForm.getBusinessType(),temp);
            }else{
                temp.getDetailList().add(detailForm);
            }
        }
        resultMap.keySet().forEach(key->{
            resultList.add(resultMap.get(key));
        });
        return resultList;
    }

    @Override
    @Transactional
    public void inventoryIn(WcabCellLoadDetailForm wcabCellLoadDetailForm) {
        WcabCellLoadForm wcabCellLoadForm=new WcabCellLoadForm();
        wcabCellLoadForm.setDeviceCode(wcabCellLoadDetailForm.getDeviceCode());
        wcabCellLoadForm.setCabCode(wcabCellLoadDetailForm.getCabCode());
        wcabCellLoadForm.setType(wcabCellLoadDetailForm.getType());
        wcabCellLoadForm.setOperationType(wcabCellLoadDetailForm.getOperationType());
        List<WcabCellLoadDetailForm> detailList=new ArrayList<>();
        detailList.add(wcabCellLoadDetailForm);
        wcabCellLoadForm.setDetailList(detailList);
        this.inventoryIn4Batch(wcabCellLoadForm);
        if (wcabCellLoadDetailForm.getUpdateWeightFlag()!=null&&wcabCellLoadDetailForm.getUpdateWeightFlag()){
            this.updateWeightUnit(wcabCellLoadForm);
        }
        Cabinet cabinet=cabinetService.getOne(new QueryWrapper<Cabinet>().eq("code",wcabCellLoadDetailForm.getCabCode()));
        String mesMsg=createCLICKMsg(CLICKConstants.INVENTORY_IN,CLICKConstants.NEW_INVENTORY_IN,JwtUtils.getUsername(),wcabCellLoadDetailForm.getSkuCode(),wcabCellLoadDetailForm.getQuantity(),cabinet.getCabName()+wcabCellLoadDetailForm.getSpaceCode());
        pushCLICKMES(mesMsg,wcabCellLoadForm.getBusinessId(),JwtUtils.getEnterpriseCode(),JwtUtils.getLoginUserId(),JwtUtils.getLoginUsername());
    }

    private String createCLICKMsg(String inventoryType,String inventoryDetailType,String userName,String skuCode,Integer quantity,String cabCode){
        return CLICKConstants.INOUT_METHOD_CODE+CLICKConstants.SEPARATOR+
                inventoryType+CLICKConstants.SEPARATOR+
                inventoryDetailType+CLICKConstants.SEPARATOR+
                userName+CLICKConstants.SEPARATOR+
                skuCode+CLICKConstants.SEPARATOR+
                quantity+CLICKConstants.SEPARATOR+
                cabCode;
    }

    private void  pushCLICKMES(String commandStr,Long businessId,String enterpriseCode,Long creator,String creatorName){
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                clickMsgService.pushMsg(commandStr,businessId,enterpriseCode,creator,creatorName);
            }
        });
    }

    private void updateWeightUnit(WcabCellLoadForm wcabCellLoadForm){
        List<WcabCellLoadDetailForm> detailList=wcabCellLoadForm.getDetailList();
        for (WcabCellLoadDetailForm wcabCellLoadDetailForm:detailList){
            ProductDetailVO productDetail=productDetailService.getProductDetailBySkuCode(wcabCellLoadDetailForm.getSkuCode());
            if (productDetail!=null){
                BigDecimal weightUnit=productDetail.getWeightNumber();
//                if (weightUnit==null||weightUnit.doubleValue()<=0){
                    if (wcabCellLoadDetailForm.getWeight()!=null&&wcabCellLoadDetailForm.getQuantity()!=null){
//                        boolean isUpdateWeightUnit=false;
//                        if (weightUnit!=null&&weightUnit.doubleValue()>0){
//                            BigDecimal quantity=wcabCellLoadDetailForm.getWeight().divide(weightUnit,mathContext);
//                            if (Math.abs(quantity.subtract(BigDecimal.valueOf(wcabCellLoadDetailForm.getQuantity())).doubleValue())>1){
//                                isUpdateWeightUnit=true;
//                            }
//                        }else{
//                            isUpdateWeightUnit=true;
//                        }
//                        if (isUpdateWeightUnit){
                            BigDecimal weight=wcabCellLoadDetailForm.getWeight().divide(BigDecimal.valueOf(wcabCellLoadDetailForm.getQuantity()),mathContext);
                            DecimalFormat df = new DecimalFormat("0.00");
                            String weightStr=df.format(weight);
                            if (!weightStr.equals(String.valueOf(productDetail.getWeightNumber()))){
                                productDetailService.updateWeightUnit(BigDecimal.valueOf(Double.valueOf(weightStr)),productDetail.getId());
                                ProductWeightRecord productWeightRecord=new ProductWeightRecord();
                                productWeightRecord.setCreator(JwtUtils.getLoginUserId());
                                productWeightRecord.setCreatorName(JwtUtils.getLoginUsername());
                                productWeightRecord.setSkuWeightCurrent(productDetail.getWeightNumber());
                                productWeightRecord.setSkuWeightAfter(BigDecimal.valueOf(Double.valueOf(weightStr)));
                                productWeightRecord.setBusinessId(wcabCellLoadForm.getId());
                                productWeightRecord.setSkuCode(wcabCellLoadDetailForm.getSkuCode());
                                productWeightRecordService.save(productWeightRecord);
                            }
                        }

//                    }
//                }
            }
        }
    }

    @Override
    @Transactional
    public void inventoryOut4Batch(OrderReceiveLoadForm orderReceiveLoadForm) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        WcabCellInventoryInoutForm cellInventoryInoutForm=new WcabCellInventoryInoutForm();
//        cellInventoryInoutForm.setReceiveLoadId(orderReceiveLoad.getId());
        List<WcabCellInventoryInoutDetailForm> list=new ArrayList<>();
//        CellInventoryInoutDetailForm cellInventoryInoutDetailForm=new CellInventoryInoutDetailForm();
//        cellInventoryInoutDetailForm.setCabCode(orderReceiveLoadForm.getCabCode());
//        cellInventoryInoutDetailForm.setSkuCode(orderReceiveLoadForm.getSkuCode());
//        cellInventoryInoutDetailForm.setSpaceCode(orderReceiveLoadForm.getSpaceCode());
//        cellInventoryInoutDetailForm.setQuantity(orderReceiveLoadForm.getQuantity());
//
//        list.add(cellInventoryInoutDetailForm);
        for(OrderReceiveLoadDetailForm orderReceiveLoadDetailForm:orderReceiveLoadForm.getDetailList()){
            WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=new WcabCellInventoryInoutDetailForm();
            wcabCellInventoryInoutDetailForm.setCabCode(orderReceiveLoadDetailForm.getCabCode());
            wcabCellInventoryInoutDetailForm.setSkuCode(orderReceiveLoadDetailForm.getSkuCode());
            wcabCellInventoryInoutDetailForm.setSpaceCode(orderReceiveLoadDetailForm.getSpaceCode());
            wcabCellInventoryInoutDetailForm.setQuantity(orderReceiveLoadDetailForm.getQuantity());
            wcabCellInventoryInoutDetailForm.setWeight(orderReceiveLoadDetailForm.getWeight());

            list.add(wcabCellInventoryInoutDetailForm);
        }
        cellInventoryInoutForm.setDetailList(list);


        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OU-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));

        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
//        wcabCellInventoryInout.setDeviceCode(deviceCode);
        wcabCellInventoryInout.setEnterpriseId(enterpriseId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        CellInventoryOutSeq cellInventoryOutSeq=new CellInventoryOutSeq();
//        cellInventoryOutSeqService.insert(cellInventoryOutSeq);
//        Long id=cellInventoryOutSeqService.getId();
//        String orderCode= StringUtils.leftPad(id.toString(),10,"0");
//        wcabCellInventoryInout.setOrderCode(orderCode);
        wcabCellInventoryInout.setRemark("自主下货");
        inventoryInoutService.save(wcabCellInventoryInout);
//        saveSkuDevice(list,deviceCode);

//        OrderDevice orderDevice=new OrderDevice();
//        orderDevice.setOrderCode(orderCode);
//        orderDevice.setDeviceCode(deviceCode);
//        orderDeviceService.save(orderDevice);

        OrderReceiveLoad orderReceiveLoad=new OrderReceiveLoad();
        BeanUtils.copyProperties(orderReceiveLoadForm,orderReceiveLoad);
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        code=dynamicService.getDayNextCode("wcab_cell_load");
//        String day=simpleDateFormat.format(new Date());
        orderReceiveLoad.setCode("UL-"+day+"-"+code);
        orderReceiveLoad.setDay(Integer.valueOf(day));
        orderReceiveLoad.setNo(Integer.valueOf(code));
        orderReceiveLoad.setCreator(JwtUtils.getLoginUserId());
        orderReceiveLoad.setCreatorName(JwtUtils.getLoginUsername());

        orderReceiveLoadService.save(orderReceiveLoad);
        saveReceiveLoadDetail(orderReceiveLoadForm,orderReceiveLoad.getId());
        wcabCellInventoryInout.setReceiveLoadId(orderReceiveLoad.getId());

        saveDetail(list,wcabCellInventoryInout.getId(),WcabCellInventoryInout.InoutType.out.name(),wcabCellInventoryInout.getBusinessType(),false);
//        useInventoryInoutDetailService.inventoryIn(list,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.cellCabReceive,null,null);
        //异步同步数据
//        CellInventoryInoutForm cellInventoryInoutForm = new CellInventoryInoutForm();
//        cellInventoryInoutForm.setDetailList(list);
//        sendMsg(wcabCellInventoryInout,cellInventoryInoutForm);
//        return wcabCellInventoryInout;
    }

    @Override
    @Transactional
    public void inventoryOut4Batch(WcabCellLoadForm wcabCellLoadForm) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String departmentName=JwtUtils.getDepartmentName();
        String roleName=JwtUtils.getRoleName();
        WcabCellInventoryInoutForm cellInventoryInoutForm=new WcabCellInventoryInoutForm();
//        cellInventoryInoutForm.setReceiveLoadId(wcabCellReceiveLoad.getId());
        List<WcabCellInventoryInoutDetailForm> list=new ArrayList<>();
//        CellInventoryInoutDetailForm cellInventoryInoutDetailForm=new CellInventoryInoutDetailForm();
//        cellInventoryInoutDetailForm.setCabCode(wcabCellLoadForm.getCabCode());
//        cellInventoryInoutDetailForm.setSkuCode(wcabCellLoadForm.getSkuCode());
//        cellInventoryInoutDetailForm.setSpaceCode(wcabCellLoadForm.getSpaceCode());
//        cellInventoryInoutDetailForm.setQuantity(wcabCellLoadForm.getQuantity());
//
//        list.add(cellInventoryInoutDetailForm);
        for(WcabCellLoadDetailForm wcabCellLoadDetailForm:wcabCellLoadForm.getDetailList()){
            WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=new WcabCellInventoryInoutDetailForm();
            wcabCellInventoryInoutDetailForm.setCabCode(wcabCellLoadDetailForm.getCabCode());
            wcabCellInventoryInoutDetailForm.setSkuCode(wcabCellLoadDetailForm.getSkuCode());
            wcabCellInventoryInoutDetailForm.setSpaceCode(wcabCellLoadDetailForm.getSpaceCode());
            wcabCellInventoryInoutDetailForm.setQuantity(wcabCellLoadDetailForm.getQuantity());
            wcabCellInventoryInoutDetailForm.setWeight(wcabCellLoadDetailForm.getWeight());

            list.add(wcabCellInventoryInoutDetailForm);
        }
        cellInventoryInoutForm.setDetailList(list);


        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OU-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));

        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(WcabCellInventoryInout.BusinessType.normal);
        wcabCellInventoryInout.setOperationType(WcabCellInventoryInout.OperationType.jsonCreator(wcabCellLoadForm.getOperationType().name()));
//        wcabCellInventoryInout.setDeviceCode(deviceCode);
        wcabCellInventoryInout.setEnterpriseId(enterpriseId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        CellInventoryOutSeq cellInventoryOutSeq=new CellInventoryOutSeq();
//        cellInventoryOutSeqService.insert(cellInventoryOutSeq);
//        Long id=cellInventoryOutSeqService.getId();
//        String orderCode= StringUtils.leftPad(id.toString(),10,"0");
//        wcabCellInventoryInout.setOrderCode(orderCode);
        wcabCellInventoryInout.setRemark("自主下货");
        wcabCellInventoryInout.setDepartmentName(departmentName);
        wcabCellInventoryInout.setRoleName(roleName);
        inventoryInoutService.save(wcabCellInventoryInout);
        wcabCellLoadForm.setBusinessId(wcabCellInventoryInout.getId());
//        saveSkuDevice(list,deviceCode);

//        OrderDevice orderDevice=new OrderDevice();
//        orderDevice.setOrderCode(orderCode);
//        orderDevice.setDeviceCode(deviceCode);
//        orderDeviceService.save(orderDevice);

        WcabCellLoad wcabCellReceiveLoad=new WcabCellLoad();
        BeanUtils.copyProperties(wcabCellLoadForm,wcabCellReceiveLoad);
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        code=dynamicService.getDayNextCode("wcab_cell_load");
//        String day=simpleDateFormat.format(new Date());
        wcabCellReceiveLoad.setCode("UL-"+day+"-"+code);
        wcabCellReceiveLoad.setDay(Integer.valueOf(day));
        wcabCellReceiveLoad.setNo(Integer.valueOf(code));
        wcabCellReceiveLoad.setCreator(JwtUtils.getLoginUserId());
        wcabCellReceiveLoad.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellReceiveLoad.setEnterpriseCode(enterpriseCode);
        wcabCellReceiveLoad.setDepartmentName(departmentName);
        wcabCellReceiveLoad.setRoleName(roleName);

        wcabCellLoadService.save(wcabCellReceiveLoad);
        saveReceiveLoadDetail(wcabCellLoadForm,wcabCellReceiveLoad.getId());
        wcabCellInventoryInout.setReceiveLoadId(wcabCellReceiveLoad.getId());
        if (wcabCellLoadForm.getIsEmpty()!=null&&wcabCellLoadForm.getIsEmpty()){
            saveDetail4Empty(list,wcabCellInventoryInout.getId(),WcabCellInventoryInout.InoutType.out.name(),wcabCellInventoryInout.getBusinessType(),false);
        }else{
            saveDetail(list,wcabCellInventoryInout.getId(),WcabCellInventoryInout.InoutType.out.name(),wcabCellInventoryInout.getBusinessType(),false);
        }

//        useInventoryInoutDetailService.inventoryIn(list,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.cellCabReceive,null,null);
        //异步同步数据
//        CellInventoryInoutForm cellInventoryInoutForm = new CellInventoryInoutForm();
//        cellInventoryInoutForm.setDetailList(list);
//        sendMsg(wcabCellInventoryInout,cellInventoryInoutForm);
//        return wcabCellInventoryInout;
    }

    @Override
    public void inventoryOut(WcabCellLoadDetailForm cellLoadDetailForm) {
        WcabCellLoadForm wcabCellLoadForm=new WcabCellLoadForm();
        wcabCellLoadForm.setDeviceCode(cellLoadDetailForm.getDeviceCode());
        wcabCellLoadForm.setCabCode(cellLoadDetailForm.getCabCode());
        wcabCellLoadForm.setType(cellLoadDetailForm.getType());
        wcabCellLoadForm.setOperationType(cellLoadDetailForm.getOperationType());
        List<WcabCellLoadDetailForm> detailList=new ArrayList<>();
        detailList.add(cellLoadDetailForm);
        wcabCellLoadForm.setDetailList(detailList);
        this.inventoryOut4Batch(wcabCellLoadForm);
        Cabinet cabinet=cabinetService.getOne(new QueryWrapper<Cabinet>().eq("code",cellLoadDetailForm.getCabCode()));
        String mesMsg=createCLICKMsg(CLICKConstants.INVENTORY_OUT,CLICKConstants.UNLOAD_INVENTORY_OUT,JwtUtils.getUsername(),cellLoadDetailForm.getSkuCode(),cellLoadDetailForm.getQuantity(),cabinet.getCabName()+cellLoadDetailForm.getSpaceCode());
        pushCLICKMES(mesMsg,wcabCellLoadForm.getBusinessId(),JwtUtils.getEnterpriseCode(),JwtUtils.getLoginUserId(),JwtUtils.getLoginUsername());
    }

    @Override
    public void inventoryOut4Empty(WcabCellLoadDetailForm cellLoadDetailForm) {
        WcabCellLoadForm wcabCellLoadForm=new WcabCellLoadForm();
        wcabCellLoadForm.setDeviceCode(cellLoadDetailForm.getDeviceCode());
        wcabCellLoadForm.setCabCode(cellLoadDetailForm.getCabCode());
        wcabCellLoadForm.setType(cellLoadDetailForm.getType());
        wcabCellLoadForm.setOperationType(cellLoadDetailForm.getOperationType());
        wcabCellLoadForm.setIsEmpty(true);
        List<WcabCellInventorySpace> inventorySpaces= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",cellLoadDetailForm.getCabCode()).eq("space_code",cellLoadDetailForm.getSpaceCode()));
        List<WcabCellLoadDetailForm> detailList=new ArrayList<>();
//        detailList.add(cellLoadDetailForm);
        inventorySpaces.forEach(e->{
            WcabCellLoadDetailForm wcabCellInventoryInoutDetailForm=new WcabCellLoadDetailForm();
            BeanUtils.copyProperties(e,wcabCellInventoryInoutDetailForm);
            detailList.add(wcabCellInventoryInoutDetailForm);
        });
        wcabCellLoadForm.setDetailList(detailList);
        this.inventoryOut4Batch(wcabCellLoadForm);
    }

    @Override
    @Transactional
    public void inventoryIn(WcabCellInventoryInoutForm cellInventoryInoutForm) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String departmentName=JwtUtils.getDepartmentName();
        String roleName=JwtUtils.getRoleName();
        if (CollectionUtils.isEmpty(cellInventoryInoutForm.getDetailList())){
            throw new BusinessException("缺少上货明细数据");
        }

//        OrderReceive orderReceive=receiveService.getById(cellInventoryInoutForm.getReceiveId());
//        if (orderReceive==null){
//            throw new BusinessException("未找到领货记录");
//        }
//        List<OrderReceiveDetail> receiveDetails=receiveDetailService.list(new QueryWrapper<OrderReceiveDetail>().eq("receive_id",cellInventoryInoutForm.getReceiveId()).eq("sku_code",cellInventoryInoutForm.getDetailList().get(0).getSkuCode()));
//        if (receiveDetails.size()==0){
//            throw new BusinessException("未找到领货明细业务数据");
//        }

        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        BeanUtils.copyProperties(cellInventoryInoutForm,wcabCellInventoryInout);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("IN-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(creator);
        wcabCellInventoryInout.setCreatorName(creatorName);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        cellInventoryInoutForm.setCreator(creator);
        cellInventoryInoutForm.setCreatorName(creatorName);
//        saveReceiveLoad(cellInventoryInoutForm);

//        saveReceiveLoad(cellInventoryInoutForm);

//        OrderReceiveLoad orderReceiveLoad=new OrderReceiveLoad();
//        BeanUtils.copyProperties(orderReceiveLoadForm,orderReceiveLoad);
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
//        code=dynamicService.getDayNextCode("ord_order_receive_load");
//        String day=simpleDateFormat.format(new Date());
//        orderReceiveLoad.setCode("RL-"+day+"-"+code);
//        orderReceiveLoad.setDay(Integer.valueOf(day));
//        orderReceiveLoad.setNo(Integer.valueOf(code));
//        orderReceiveLoad.setCreator(JwtUtils.getLoginUserId());
//        orderReceiveLoad.setCreatorName(JwtUtils.getLoginUsername());
//        orderReceiveLoad.setReceiveId(cellInventoryInoutForm.getReceiveId());
//        orderReceiveLoad.setReceiveDetailId(receiveDetails.get(0).getId());
//        orderReceiveLoad.setSkuCode(cellInventoryInoutForm.getDetailList().get(0).getSkuCode());
//        orderReceiveLoad.setCabCode(cellInventoryInoutForm.getDetailList().get(0).getCabCode());
//        orderReceiveLoad.setSpaceCode(cellInventoryInoutForm.getDetailList().get(0).getSpaceCode());
//
//        orderReceiveLoad.setSkuId(cellInventoryInoutForm.getDetailList().get(0).getSkuId());
//        orderReceiveLoad.setCabId(cellInventoryInoutForm.getDetailList().get(0).getCabId());
//        orderReceiveLoad.setSpaceId(cellInventoryInoutForm.getDetailList().get(0).getSpaceId());
//        orderReceiveLoad.setQuantity(cellInventoryInoutForm.getDetailList().get(0).getQuantity());
//        orderReceiveLoad.setType(OrderReceiveLoad.Type.jsonCreator(orderReceive.getType().name()));
//        orderReceiveLoadService.save(orderReceiveLoad);
//        wcabCellInventoryInout.setReceiveLoadId(orderReceiveLoad.getId());

        wcabCellInventoryInout.setEnterpriseId(enterpriseId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
        wcabCellInventoryInout.setDepartmentName(departmentName);
        wcabCellInventoryInout.setRoleName(roleName);
        inventoryInoutService.save(wcabCellInventoryInout);
        saveDetail(cellInventoryInoutForm.getDetailList(),wcabCellInventoryInout.getId(),CellInventoryInout.Type.in.name(),cellInventoryInoutForm.getBusinessType(),false);
        if(WcabCellInventoryInout.OperationType.recycleIn.equals(cellInventoryInoutForm.getOperationType())||
                WcabCellInventoryInout.OperationType.repairIn.equals(cellInventoryInoutForm.getOperationType())||
                WcabCellInventoryInout.OperationType.returnIn.equals(cellInventoryInoutForm.getOperationType())
        ){
            useInventoryInoutDetailService.inventoryOut(cellInventoryInoutForm.getDetailList(),wcabCellInventoryInout.getId(),cellInventoryInoutForm.getDeviceCode(),WcabCellInventoryInout.BusinessType.jsonCreator(cellInventoryInoutForm.getBusinessType().name()));
        }
        //异步同步数据
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
//        sendMsg(wcabCellInventoryInout,cellInventoryInoutForm);
        Cabinet cabinet=cabinetService.getOne(new QueryWrapper<Cabinet>().eq("code",cellInventoryInoutForm.getDetailList().get(0).getCabCode()));
        if (WcabCellInventoryInout.OperationType.returnIn.equals(cellInventoryInoutForm.getOperationType())){
            cellInventoryInoutForm.getDetailList().forEach(e->{
                String mesMsg=createCLICKMsg(CLICKConstants.INVENTORY_IN,CLICKConstants.RETURN_INVENTORY_IN,JwtUtils.getUsername(),e.getSkuCode(),e.getQuantity(),cabinet.getCabName()+e.getSpaceCode());
                pushCLICKMES(mesMsg,wcabCellInventoryInout.getId(),enterpriseCode,JwtUtils.getLoginUserId(),JwtUtils.getLoginUsername());
            });

        }

    }

    @Override
    public void inventoryOut(WcabCellInventoryInoutForm cellInventoryInoutForm) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isEmpty(cellInventoryInoutForm.getDetailList())){
            throw new BusinessException("缺少上货明细数据");
        }
//        OrderReceive orderReceive=receiveService.getById(cellInventoryInoutForm.getReceiveId());
//        if (orderReceive==null){
//            throw new BusinessException("未找到领货记录");
//        }
//        List<OrderReceiveDetail> receiveDetails=receiveDetailService.list(new QueryWrapper<OrderReceiveDetail>().eq("receive_id",cellInventoryInoutForm.getReceiveId()).eq("sku_code",cellInventoryInoutForm.getDetailList().get(0).getSkuCode()));
//        if (receiveDetails.size()==0){
//            throw new BusinessException("未找到领货明细业务数据");
//        }

        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        BeanUtils.copyProperties(cellInventoryInoutForm,wcabCellInventoryInout);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OU-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(creator);
        wcabCellInventoryInout.setCreatorName(creatorName);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        cellInventoryInoutForm.setCreator(creator);
        cellInventoryInoutForm.setCreatorName(creatorName);
        wcabCellInventoryInout.setEnterpriseId(enterpriseId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
        inventoryInoutService.save(wcabCellInventoryInout);
        saveDetail(cellInventoryInoutForm.getDetailList(),wcabCellInventoryInout.getId(),CellInventoryInout.Type.out.name(),cellInventoryInoutForm.getBusinessType(),false);
        //异步同步数据
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
    }

    @Override
    @Transactional
    public WcabCellInventoryInout inventoryOut(List<WcabCellInventoryInoutDetailForm> list,String deviceCode,WcabCellInventoryInout.BusinessType businessType,WcabCellInventoryInout.OperationType operationType,Long storesRequisitionId) {
        //校验权限组
//        verifyPermissions(list);
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String enterpriseName=JwtUtils.getEnterpriseName();
//        List<SystemRuleInfo> systemRuleInfos=systemRuleInfoService.list();
        SystemRuleInfo systemRuleInfo=systemRuleInfoService.getRuleInfo();
        if (systemRuleInfo!=null&&systemRuleInfo.getTotalFlag()){
//            saveInventoryOutAmount(list,systemRuleInfos.get(0).getTotal());
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OU-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));

        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setOperationType(operationType);
        wcabCellInventoryInout.setDeviceCode(deviceCode);
        wcabCellInventoryInout.setEnterpriseId(enterpriseId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setRemark(businessType.getDesc());
        WcabCellInventoryOutSeq cellInventoryOutSeq=new WcabCellInventoryOutSeq();
        cellInventoryOutSeqService.insert(cellInventoryOutSeq);
        Long id=cellInventoryOutSeqService.getId();
        String orderCode= StringUtils.leftPad(id.toString(),10,"0");
        wcabCellInventoryInout.setOrderCode(orderCode);

        inventoryInoutService.save(wcabCellInventoryInout);
//        saveSkuDevice(list,deviceCode);

//        OrderDevice orderDevice=new OrderDevice();
//        orderDevice.setOrderCode(orderCode);
//        orderDevice.setDeviceCode(deviceCode);
//        orderDeviceService.save(orderDevice);

        saveDetail(list,wcabCellInventoryInout.getId(),CellInventoryInout.Type.out.name(),businessType,false);
        WcabUseInventoryInout useInventoryInout=useInventoryInoutDetailService.inventoryIn(list,wcabCellInventoryInout.getId(),businessType,deviceCode,null);
//        wcabCellInventoryInout.setLabelCodeList(useInventoryInout.getLabelCodeList());
        //异步同步数据
        WcabCellInventoryInoutForm cellInventoryInoutForm = new WcabCellInventoryInoutForm();
        cellInventoryInoutForm.setDetailList(list);

        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
//        sendMsg(wcabCellInventoryInout,cellInventoryInoutForm);
//        return wcabCellInventoryInout;
        if (storesRequisitionId!=null&&storesRequisitionId!=0){
            storesRequisitionService.requisitionProductRecord(storesRequisitionId,getRequestProduct(list));
        }
        Cabinet cabinet=cabinetService.getOne(new QueryWrapper<Cabinet>().eq("code",list.get(0).getCabCode()));
        list.forEach(e->{
            String mesMsg=createCLICKMsg(CLICKConstants.INVENTORY_OUT,CLICKConstants.NEW_INVENTORY_OUT,JwtUtils.getUsername(),e.getSkuCode(),e.getQuantity(),cabinet.getCabName()+e.getSpaceCode());
            pushCLICKMES(mesMsg,wcabCellInventoryInout.getId(),enterpriseCode,JwtUtils.getLoginUserId(),JwtUtils.getLoginUsername());
        });
        return  wcabCellInventoryInout;
    }

    @Override
    @Transactional
    public WcabCellInventoryInout inventoryOut(String cabCode,String spaceCode,WcabCellInventoryInout.OperationType operationType,String deviceCode) {
        List<WcabCellInventorySpace> inventorySpaces= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",cabCode).eq("space_code",spaceCode));
        List<WcabCellInventoryInoutDetailForm> list=new ArrayList<>();
        if (inventorySpaces.size()==0){
            return null;
        }
        WcabCellInventoryInout.BusinessType businessType=WcabCellInventoryInout.BusinessType.jsonCreator(inventorySpaces.get(0).getBusinessType().name());
        inventorySpaces.forEach(e->{
            WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=new WcabCellInventoryInoutDetailForm();
            BeanUtils.copyProperties(e,wcabCellInventoryInoutDetailForm);
            list.add(wcabCellInventoryInoutDetailForm);
        });
        //校验权限组
//        verifyPermissions(list);
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String enterpriseName=JwtUtils.getEnterpriseName();
//        List<SystemRuleInfo> systemRuleInfos=systemRuleInfoService.list();
        SystemRuleInfo systemRuleInfo=systemRuleInfoService.getRuleInfo();
        if (systemRuleInfo!=null&&systemRuleInfo.getTotalFlag()){
//            saveInventoryOutAmount(list,systemRuleInfos.get(0).getTotal());
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OU-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));

        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setOperationType(operationType);
        wcabCellInventoryInout.setDeviceCode(deviceCode);
        wcabCellInventoryInout.setEnterpriseId(enterpriseId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setRemark(businessType.getDesc());
        WcabCellInventoryOutSeq cellInventoryOutSeq=new WcabCellInventoryOutSeq();
        cellInventoryOutSeqService.insert(cellInventoryOutSeq);
        Long id=cellInventoryOutSeqService.getId();
        String orderCode= StringUtils.leftPad(id.toString(),10,"0");
        wcabCellInventoryInout.setOrderCode(orderCode);

        inventoryInoutService.save(wcabCellInventoryInout);
//        saveSkuDevice(list,deviceCode);

//        OrderDevice orderDevice=new OrderDevice();
//        orderDevice.setOrderCode(orderCode);
//        orderDevice.setDeviceCode(deviceCode);
//        orderDeviceService.save(orderDevice);

        saveDetail4Empty(list,wcabCellInventoryInout.getId(),CellInventoryInout.Type.out.name(),businessType,false);
        
//        wcabCellInventoryInout.setLabelCodeList(useInventoryInout.getLabelCodeList());
        //异步同步数据
        WcabCellInventoryInoutForm cellInventoryInoutForm = new WcabCellInventoryInoutForm();
        cellInventoryInoutForm.setDetailList(list);

        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);
//        sendMsg(wcabCellInventoryInout,cellInventoryInoutForm);
//        return wcabCellInventoryInout;

        return  wcabCellInventoryInout;
    }

    private List<StoresRequisitionProductForm> getRequestProduct(List<WcabCellInventoryInoutDetailForm> list){
        List<StoresRequisitionProductForm> resultList=new ArrayList<>();
        Map<String,StoresRequisitionProductForm> resultMap=new HashMap<>();
        for (WcabCellInventoryInoutDetailForm cellInventoryInoutDetailForm:list){
            StoresRequisitionProductForm temp=resultMap.get(cellInventoryInoutDetailForm.getSkuCode());
            if (temp==null){
                StoresRequisitionProductForm storesRequisitionProductForm=new StoresRequisitionProductForm();
                storesRequisitionProductForm.setSkuCode(cellInventoryInoutDetailForm.getSkuCode());
                storesRequisitionProductForm.setNeckQuantity(cellInventoryInoutDetailForm.getQuantity());
                storesRequisitionProductForm.setId(cellInventoryInoutDetailForm.getRequestProductId());
                resultMap.put(cellInventoryInoutDetailForm.getSkuCode(),storesRequisitionProductForm);
            }else{
                temp.setNeckQuantity(temp.getNeckQuantity()+cellInventoryInoutDetailForm.getQuantity());
                resultMap.put(cellInventoryInoutDetailForm.getSkuCode(),temp);
            }
        }
        resultMap.keySet().forEach(item->{
            resultList.add(resultMap.get(item));
        });
        return resultList;
    }

    @Transactional
    void saveReceiveLoadDetail(OrderReceiveLoadForm orderReceiveLoadForm,Long loadId){
        if(CollectionUtil.isNotEmpty(orderReceiveLoadForm.getDetailList())){
            for (OrderReceiveLoadDetailForm orderReceiveLoadDetailForm:orderReceiveLoadForm.getDetailList()){
                OrderReceiveLoadDetail orderReceiveLoadDetail=new OrderReceiveLoadDetail();
                BeanUtils.copyProperties(orderReceiveLoadDetailForm,orderReceiveLoadDetail);
                orderReceiveLoadDetail.setLoadId(loadId);
                orderReceiveLoadDetailService.save(orderReceiveLoadDetail);
            }
        }
    }

    @Transactional
    void saveReceiveLoadDetail(WcabCellLoadForm wcabCellLoadForm,Long loadId){
        if(CollectionUtil.isNotEmpty(wcabCellLoadForm.getDetailList())){
            for (WcabCellLoadDetailForm wcabCellLoadDetailForm:wcabCellLoadForm.getDetailList()){
                WcabCellLoadDetail wcabCellLoadDetail=new WcabCellLoadDetail();
                BeanUtils.copyProperties(wcabCellLoadDetailForm,wcabCellLoadDetail);
                wcabCellLoadDetail.setLoadId(loadId);
                wcabCellLoadDetailService.save(wcabCellLoadDetail);
            }
        }
    }


    public JsonResult isInventoryIn(WcabCellInventoryInoutDetailForm cellInventoryInoutDetailForm) {
        List<CabinetCargoLane> lanes=laneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",cellInventoryInoutDetailForm.getCabCode()).eq("code",cellInventoryInoutDetailForm.getSpaceCode()));
        if (lanes.size()==0){
            return JsonResult.fail("未找到货道");
        }
        List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",cellInventoryInoutDetailForm.getCabCode()).eq("space_code",cellInventoryInoutDetailForm.getSpaceCode()));
        if (spaceInventoryList.size()>0){
            if (spaceInventoryList.get(0).getWeight()!=null&&spaceInventoryList.get(0).getWeight().doubleValue()>0&&!spaceInventoryList.get(0).getSkuCode().equals(cellInventoryInoutDetailForm.getSkuCode())){
                return JsonResult.fail("此货位"+cellInventoryInoutDetailForm.getSpaceCode()+"已经存放了SKU编码:["+spaceInventoryList.get(0).getSkuCode()+"]的刀具,"+"不可以在存放SKU编码:["+cellInventoryInoutDetailForm.getSkuCode()+"]的刀具");
            }
            BigDecimal laneWeight=lanes.get(0).getMaxBearing();
            if (laneWeight!=null){
                BigDecimal inventoryWeight=spaceInventoryList.get(0).getWeight();
                BigDecimal surplusQuantity=laneWeight.subtract(inventoryWeight);
                if (surplusQuantity.subtract(cellInventoryInoutDetailForm.getWeight()).doubleValue()<0){
                    return JsonResult.fail("此货道剩余可上货重量不够");
                }
            }

        }
        return JsonResult.success();
    }

    private BigDecimal getSumWeightBySpace(List<WcabCellInventorySpace> spaceInventoryList){
        BigDecimal sum=BigDecimal.valueOf(0);
        if(CollectionUtil.isEmpty(spaceInventoryList)){
            return sum;
        }

        for (WcabCellInventorySpace storageInventorySpace:spaceInventoryList){
            sum=sum.add(storageInventorySpace.getWeight());
        }
        return sum;
    }



    @Transactional
    void saveCheckData(List<WcabCellInventoryInoutDetailForm> list, WcabCellInventoryInout.BusinessType businessType, Long inoutId, StorageInventoryInout.BusinessDetailType businessDetailType){
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        List<WcabCellInventoryCheck> storageInventoryChecks= storageInventoryCheckService.list(new QueryWrapper<WcabCellInventoryCheck>().eq("cab_code",list.get(0).getCabCode()).eq("space_code",list.get(0).getSpaceCode()).eq("status",StorageInventoryCheck.Status.created.getValue()));
        Long checkId=null;
        boolean isFirst=false;
        if (storageInventoryChecks.size()==0){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
            WcabCellInventoryCheck wcabCellInventoryCheck=new WcabCellInventoryCheck();
            String code=dynamicService.getDayNextCode("wcab_cell_inventory_check");
            String day=simpleDateFormat.format(new Date());
            wcabCellInventoryCheck.setCode("CH-"+day+"-"+code);
            wcabCellInventoryCheck.setDay(Integer.valueOf(day));
            wcabCellInventoryCheck.setNo(Integer.valueOf(code));
            wcabCellInventoryCheck.setCreator(JwtUtils.getLoginUserId());
            wcabCellInventoryCheck.setCreatorName(JwtUtils.getLoginUsername());
            wcabCellInventoryCheck.setOperationType(WcabCellInventoryCheck.OperationType.in);
            wcabCellInventoryCheck.setBusinessType(WcabCellInventoryCheck.BusinessType.jsonCreator(businessType.name()));
            wcabCellInventoryCheck.setStatus(WcabCellInventoryCheck.Status.created);
            wcabCellInventoryCheck.setCabCode(list.get(0).getCabCode());
            wcabCellInventoryCheck.setSpaceCode(list.get(0).getSpaceCode());
//            wcabCellInventoryCheck.setBusinessDetailType(WcabCellInventoryCheck.BusinessDetailType.jsonCreator(businessDetailType.name()));
//            wcabCellInventoryCheck.setRemark(wcabCellInventoryCheck.getBusinessDetailTypeDesc());
            storageInventoryCheckService.save(wcabCellInventoryCheck);
            checkId=wcabCellInventoryCheck.getId();
            isFirst=true;
        }else{
            checkId=storageInventoryChecks.get(0).getId();
        }
        for (WcabCellInventoryInoutDetailForm useInventoryInoutDetailForm:list){
            if (isFirst){
                saveCheckDetail(useInventoryInoutDetailForm,creator,creatorName,checkId,businessType);
            }else{
                List<WcabCellInventoryCheckDetail> storageInventoryCheckDetails= wcabCellInventoryCheckDetailService.list(new QueryWrapper<WcabCellInventoryCheckDetail>().eq("check_id",checkId).eq("sku_code",useInventoryInoutDetailForm.getSkuCode()));
                if (storageInventoryCheckDetails.size()==0){
                    saveCheckDetail(useInventoryInoutDetailForm,creator,creatorName,checkId,businessType);
                }else{
                    WcabCellInventoryCheckDetail wcabCellInventoryCheckDetail4Update=new WcabCellInventoryCheckDetail();
                    wcabCellInventoryCheckDetail4Update.setQuantity(storageInventoryCheckDetails.get(0).getQuantity()+useInventoryInoutDetailForm.getQuantity());
                    wcabCellInventoryCheckDetail4Update.setId(storageInventoryCheckDetails.get(0).getId());
                    wcabCellInventoryCheckDetailService.updateById(wcabCellInventoryCheckDetail4Update);
                }
            }
        }
        WcabCellInventoryCheckInout wcabCellInventoryCheckInout=new WcabCellInventoryCheckInout();
        wcabCellInventoryCheckInout.setCheckId(checkId);
        wcabCellInventoryCheckInout.setInoutId(inoutId);
        wcabCellInventoryCheckInoutService.save(wcabCellInventoryCheckInout);

    }
    @Transactional
    void saveCheckDetail(WcabCellInventoryInoutDetailForm useInventoryInoutDetailForm,Long creator,String creatorName,Long checkId,WcabCellInventoryInout.BusinessType businessType){
        Long enterpriseId=JwtUtils.getEnterpriseId();
        List<WcabCellInventoryCheckDetail> checkDetails= wcabCellInventoryCheckDetailService.list(new QueryWrapper<WcabCellInventoryCheckDetail>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",useInventoryInoutDetailForm.getSkuCode()));
        if (checkDetails.size()==0){
            WcabCellInventoryCheckDetail wcabCellInventoryCheckDetail=new WcabCellInventoryCheckDetail();
            BeanUtils.copyProperties(useInventoryInoutDetailForm,wcabCellInventoryCheckDetail);
            wcabCellInventoryCheckDetail.setCompanyId(enterpriseId);
            wcabCellInventoryCheckDetail.setId(null);
            wcabCellInventoryCheckDetail.setCreator(creator);
            wcabCellInventoryCheckDetail.setCreatorName(creatorName);
            wcabCellInventoryCheckDetail.setOperationType(WcabCellInventoryCheckDetail.OperationType.in);
            wcabCellInventoryCheckDetail.setBusinessType(WcabCellInventoryCheckDetail.BusinessType.jsonCreator(businessType.name()));
            wcabCellInventoryCheckDetail.setCheckId(checkId);
            wcabCellInventoryCheckDetailService.save(wcabCellInventoryCheckDetail);
        }else{
            WcabCellInventoryCheckDetail wcabCellInventoryCheckDetail4Update=new WcabCellInventoryCheckDetail();
            wcabCellInventoryCheckDetail4Update.setId(checkDetails.get(0).getId());
            wcabCellInventoryCheckDetail4Update.setQuantity(checkDetails.get(0).getQuantity()+useInventoryInoutDetailForm.getQuantity());
            wcabCellInventoryCheckDetailService.updateById(wcabCellInventoryCheckDetail4Update);
        }


    }

    @Transactional
    void saveDetail(List<WcabCellInventoryInoutDetailForm> list,Long inventoryInoutId,String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,Boolean isCheck){
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isNotEmpty(list)){
            if (WcabUseInventoryInout.InoutType.in.name().equals(inOutTypeName)&&
                    !isCheck&&
                    (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                saveCheckData(list,businessType,inventoryInoutId,null);
            }
            for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabCellInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setEnterpriseId(enterpriseId);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
//                wcabCellInventoryInoutDetail.setType(WcabCellInventoryInoutDetail.Type.jsonCreator(inOutTypeName));
//                wcabCellInventoryInoutDetail.setbu
                wcabCellInventoryInoutDetail.setInoutId(inventoryInoutId);
                List<CabinetCargoLane> cabinetCargoSpaces=laneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoSpaces.size()==0){
                    throw new BusinessException("未找到货道");
                }

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));

                if (WcabUseInventoryInout.Type.in.name().equals(inOutTypeName)){


                    log.info("{},{}",creator,cabinetCargoSpaces.get(0).getPrincipal());
                    if(cabinetCargoSpaces.get(0).getPrincipal()!=null&&cabinetCargoSpaces.get(0).getPrincipal()!=0){
                        if (!creator.equals(cabinetCargoSpaces.get(0).getPrincipal())){
                            throw new BusinessException("柜子编号["+cabinetCargoSpaces.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
                        }

                    }
                    if (StringUtils.isNotBlank(cabinetCargoSpaces.get(0).getSkuCode())){
                        if (!wcabCellInventoryInoutDetailForm.getSkuCode().equals(cabinetCargoSpaces.get(0).getSkuCode())){
                            throw new BusinessException("此库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]不能存储此产品SKU编号["+wcabCellInventoryInoutDetailForm.getSkuCode()+"]的产品，只能存放SKU编号为"+cabinetCargoSpaces.get(0).getSkuCode()+"的产品");
                        }
                    }
                    if (cabinetCargoSpaces.get(0).getSpaceProductType()!=null&&cabinetCargoSpaces.get(0).getSpaceProductType()!=0){
                        Map paramMap=new HashMap();
                        paramMap.put("skuCode4app",wcabCellInventoryInoutDetailForm.getSkuCode());
                        List<ProductDetailVO> productDetailVOS=productDetailService.list4app(paramMap);
                        if (productDetailVOS.size()==0){
                            throw new BusinessException("未找到产品");
                        }
                        if(!cabinetCargoSpaces.get(0).getSpaceProductType().equals(productDetailVOS.get(0).getProductTypeId())){
                            throw new BusinessException("此库位不能存储此产品类型["+productDetailVOS.get(0).getProductTypeName()+"]");
                        }

                    }
                    if (cabinetCargoSpaces.get(0).getSpaceType()!=null&&!CabinetCargoSpace.SpaceType.common.equals(cabinetCargoSpaces.get(0).getSpaceType())){
                        if (!cabinetCargoSpaces.get(0).getSpaceType().name().equals(businessType.name())
                        ){
                            throw new BusinessException("此库位只能存贮["+cabinetCargoSpaces.get(0).getSpaceType().getDesc()+"],不能再存贮["+businessType.getDesc()+"]");
                        }
                    }
                    if (spaceInventoryList.size()>0&&!spaceInventoryList.get(0).getBusinessType().name().equals(businessType.name())){
                        throw new BusinessException("此库位当前存贮的是["+spaceInventoryList.get(0).getBusinessType().getDesc()+"]产品,不能再存贮["+businessType.getDesc()+"]产品");
                    }

                    if (cabinetCargoSpaces.get(0).getMaxBearing()!=null){
                        if (CollectionUtil.isEmpty(spaceInventoryList)){
                            BigDecimal surplusWeight=cabinetCargoSpaces.get(0).getMaxBearing().subtract(wcabCellInventoryInoutDetailForm.getWeight());
                            if (surplusWeight.doubleValue()<0){
                                throw new BusinessException("上货重量["+wcabCellInventoryInoutDetailForm.getWeight()+"]超过此货道设置的最大上货重量["+cabinetCargoSpaces.get(0).getMaxBearing()+"]");
                            }
                        }
                        BigDecimal sum=getSumWeightBySpace(spaceInventoryList);
                        sum=sum.add(wcabCellInventoryInoutDetailForm.getWeight());
                        if (cabinetCargoSpaces.get(0).getMaxBearing().subtract(sum).doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"]库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]已经存满");
                        }
                    }
//                    List<WcabCellInventoryUser> skuInventoryUserList=cellInventoryUserService.list(new QueryWrapper<WcabCellInventoryUser>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
//                    if (
//                            (!spaceUserShareFlag&&!StorageInventoryInout.BusinessType.temp.equals(businessType))||
//                            (!spaceTempUserShareFlag&&StorageInventoryInout.BusinessType.temp.equals(businessType))&&
//                                    skuInventoryUserList.size()>0&&!creator.equals(skuInventoryUserList.get(0).getCreator())){
//                        throw new BusinessException("此货道已被他人占用");
//                    }
                }
                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                ProductDetail.InventoryUnitType inventoryUnitType=null;
                if (productDetailVO!=null){
                    inventoryUnitType=productDetailVO.getInventoryUnitType();
                }
                List<WcabCellInventorySpace> spaceSkuList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList= storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                List<WcabCellInventoryProduct> skuInventoryList= inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                if (WcabCellInventoryInout.InoutType.out.name().equals(inOutTypeName)
//                        ||WcabCellInventoryInout.InoutType.unload.name().equals(inOutTypeName)
                ){
//                    if (spaceSkuList.size()==0){
//                        throw new BusinessException("未找到柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品");
//                    }else{

                        Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                        BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                            if (quantity<=0){
                                throw new BusinessException("领取数量参数异常");
                            }
                        }else{
                            if (weight.doubleValue()<=0){
                                throw new BusinessException("领取重量参数异常");
                            }
                        }


                        if (quantity>0){
                            quantity=-1*quantity;
                        }
                        if (weight.doubleValue()>0){
                            weight=weight.multiply(BigDecimal.valueOf(-1));
                        }
                        if (spaceInventoryList.size()>0){
                            wcabCellInventoryInoutDetailForm.setSkuCode(spaceSkuList.get(0).getSkuCode());
                            Integer currentQuantity=spaceSkuList.get(0).getQuantity();
                            BigDecimal currentWeight=spaceSkuList.get(0).getWeight();
                            Integer newQuantity=currentQuantity+quantity;
                            BigDecimal newWeight=currentWeight.add(weight);
                            if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (newQuantity<0){
//                                throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                                }
                            }else{
                                if (newWeight.doubleValue()<0){
//                                throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                                }
                            }

                            WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                            wcabCellInventorySpace.setId(spaceSkuList.get(0).getId());
                            wcabCellInventorySpace.setQuantity(newQuantity);
                            wcabCellInventorySpace.setWeight(newWeight);
                            if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (newQuantity<=0){
                                    inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                                }else{
                                    inventorySpaceService.updateById(wcabCellInventorySpace);
                                }
                            }else{
                                if (newWeight.doubleValue()<=0){
                                    inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                                }else{
                                    inventorySpaceService.updateById(wcabCellInventorySpace);
                                }
                            }
                            wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                            wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                            wcabCellInventoryInoutDetail.setQuantity(quantity);
                            wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                            wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        }



                        if(skuSingleInventoryList.size()>0){
                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                            wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                            wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                            if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (wcabCellSingleInventoryProduct.getQuantity()<=0){
                                    storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                                }else{
                                    storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                                }
                            }else{
                                if (wcabCellSingleInventoryProduct.getWeight().doubleValue()<=0){
                                    storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                                }else{
                                    storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                                }
                            }

                        }

                        if(skuInventoryList.size()>0){
                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                            wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                            wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                            if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (wcabCellInventoryProduct.getQuantity()<=0){
                                    inventoryProductService.removeById(skuInventoryList.get(0).getId());
                                }else{
                                    inventoryProductService.updateById(wcabCellInventoryProduct);
                                }
                            }else{
                                if (wcabCellInventoryProduct.getWeight().doubleValue()<=0){
                                    inventoryProductService.removeById(skuInventoryList.get(0).getId());
                                }else{
                                    inventoryProductService.updateById(wcabCellInventoryProduct);
                                }
                            }

                        }


                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.out);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
//                    }
                }else if(WcabCellInventoryInout.InoutType.in.name().equals(inOutTypeName)){

                    JsonResult usableResult=isInventoryIn(wcabCellInventoryInoutDetailForm);
                    if (JsonResult.Code.SUCCESS.getCode()!=usableResult.getCode()){
                        throw new BusinessException(usableResult.getMsg());
                    }
                    Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                    if (quantity<=0){
                        throw new BusinessException("上货数量参数异常");
                    }
                    BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                    if (weight.doubleValue()<0){
                        throw new BusinessException("上货重量参数异常");
                    }
                    if (spaceInventoryList.size()==0){
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setQuantity(quantity);
                        wcabCellInventorySpace.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventorySpace.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());
                        wcabCellInventorySpace.setWeight(weight);
                        wcabCellInventorySpace.setEnterpriseId(enterpriseId);
                        wcabCellInventorySpace.setEnterpriseCode(enterpriseCode);
                        wcabCellInventorySpace.setBusinessType(WcabCellInventorySpace.BusinessType.jsonCreator(businessType.name()));
                        inventorySpaceService.save(wcabCellInventorySpace);

                        wcabCellInventoryInoutDetail.setQuantityCurrent(0);
                        wcabCellInventoryInoutDetail.setQuantityAfter(quantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setWeightAfter(weight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }else{
                        Integer currentQuantity=spaceInventoryList.get(0).getQuantity();
                        Integer newQuantity=currentQuantity+quantity;
                        BigDecimal currentWeight=spaceInventoryList.get(0).getWeight();
                        BigDecimal newWeight=currentWeight.add(weight);

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);
                        wcabCellInventorySpace.setWeight(newWeight);
//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                    if (skuSingleInventoryList.size()==0){
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(quantity);
                        wcabCellSingleInventoryProduct.setWeight(weight);
                        wcabCellSingleInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellSingleInventoryProduct.setBusinessType(WcabCellSingleInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        storageSingleInventoryProductService.save(wcabCellSingleInventoryProduct);
                    }else{
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                        wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                    }

                    if (skuInventoryList.size()==0){
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(quantity);
                        wcabCellInventoryProduct.setWeight(weight);
                        wcabCellInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellInventoryProduct.setBusinessType(WcabCellInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        inventoryProductService.save(wcabCellInventoryProduct);
                    }else{
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                        wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                        inventoryProductService.updateById(wcabCellInventoryProduct);
                    }
                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交领用物料数据");
        }
    }

    @Transactional
    void saveDetail4Empty(List<WcabCellInventoryInoutDetailForm> list,Long inventoryInoutId,String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,Boolean isCheck){
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isNotEmpty(list)){
            if (WcabUseInventoryInout.InoutType.in.name().equals(inOutTypeName)&&
                    !isCheck&&
                    (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                saveCheckData(list,businessType,inventoryInoutId,null);
            }
            for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabCellInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setEnterpriseId(enterpriseId);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
//                wcabCellInventoryInoutDetail.setType(WcabCellInventoryInoutDetail.Type.jsonCreator(inOutTypeName));
//                wcabCellInventoryInoutDetail.setbu
                wcabCellInventoryInoutDetail.setInoutId(inventoryInoutId);
                List<CabinetCargoLane> cabinetCargoSpaces=laneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoSpaces.size()==0){
                    throw new BusinessException("未找到货道");
                }

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));

                if (WcabUseInventoryInout.Type.in.name().equals(inOutTypeName)){


                    log.info("{},{}",creator,cabinetCargoSpaces.get(0).getPrincipal());
                    if(cabinetCargoSpaces.get(0).getPrincipal()!=null&&cabinetCargoSpaces.get(0).getPrincipal()!=0){
                        if (!creator.equals(cabinetCargoSpaces.get(0).getPrincipal())){
                            throw new BusinessException("柜子编号["+cabinetCargoSpaces.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
                        }

                    }
                    if (StringUtils.isNotBlank(cabinetCargoSpaces.get(0).getSkuCode())){
                        if (!wcabCellInventoryInoutDetailForm.getSkuCode().equals(cabinetCargoSpaces.get(0).getSkuCode())){
                            throw new BusinessException("此库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]不能存储此产品SKU编号["+wcabCellInventoryInoutDetailForm.getSkuCode()+"]的产品，只能存放SKU编号为"+cabinetCargoSpaces.get(0).getSkuCode()+"的产品");
                        }
                    }
                    if (cabinetCargoSpaces.get(0).getSpaceProductType()!=null&&cabinetCargoSpaces.get(0).getSpaceProductType()!=0){
                        Map paramMap=new HashMap();
                        paramMap.put("skuCode4app",wcabCellInventoryInoutDetailForm.getSkuCode());
                        List<ProductDetailVO> productDetailVOS=productDetailService.list4app(paramMap);
                        if (productDetailVOS.size()==0){
                            throw new BusinessException("未找到产品");
                        }
                        if(!cabinetCargoSpaces.get(0).getSpaceProductType().equals(productDetailVOS.get(0).getProductTypeId())){
                            throw new BusinessException("此库位不能存储此产品类型["+productDetailVOS.get(0).getProductTypeName()+"]");
                        }

                    }
                    if (cabinetCargoSpaces.get(0).getSpaceType()!=null&&!CabinetCargoSpace.SpaceType.common.equals(cabinetCargoSpaces.get(0).getSpaceType())){
                        if (!cabinetCargoSpaces.get(0).getSpaceType().name().equals(businessType.name())
                        ){
                            throw new BusinessException("此库位只能存贮["+cabinetCargoSpaces.get(0).getSpaceType().getDesc()+"],不能再存贮["+businessType.getDesc()+"]");
                        }
                    }
                    if (spaceInventoryList.size()>0&&!spaceInventoryList.get(0).getBusinessType().name().equals(businessType.name())){
                        throw new BusinessException("此库位当前存贮的是["+spaceInventoryList.get(0).getBusinessType().getDesc()+"]产品,不能再存贮["+businessType.getDesc()+"]产品");
                    }

                    if (cabinetCargoSpaces.get(0).getMaxBearing()!=null){
                        if (CollectionUtil.isEmpty(spaceInventoryList)){
                            BigDecimal surplusWeight=cabinetCargoSpaces.get(0).getMaxBearing().subtract(wcabCellInventoryInoutDetailForm.getWeight());
                            if (surplusWeight.doubleValue()<0){
                                throw new BusinessException("上货重量["+wcabCellInventoryInoutDetailForm.getWeight()+"]超过此货道设置的最大上货重量["+cabinetCargoSpaces.get(0).getMaxBearing()+"]");
                            }
                        }
                        BigDecimal sum=getSumWeightBySpace(spaceInventoryList);
                        sum=sum.add(wcabCellInventoryInoutDetailForm.getWeight());
                        if (cabinetCargoSpaces.get(0).getMaxBearing().subtract(sum).doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"]库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]已经存满");
                        }
                    }
//                    List<WcabCellInventoryUser> skuInventoryUserList=cellInventoryUserService.list(new QueryWrapper<WcabCellInventoryUser>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
//                    if (
//                            (!spaceUserShareFlag&&!StorageInventoryInout.BusinessType.temp.equals(businessType))||
//                            (!spaceTempUserShareFlag&&StorageInventoryInout.BusinessType.temp.equals(businessType))&&
//                                    skuInventoryUserList.size()>0&&!creator.equals(skuInventoryUserList.get(0).getCreator())){
//                        throw new BusinessException("此货道已被他人占用");
//                    }
                }
                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                ProductDetail.InventoryUnitType inventoryUnitType=null;
                if (productDetailVO!=null){
                    inventoryUnitType=productDetailVO.getInventoryUnitType();
                }
                List<WcabCellInventorySpace> spaceSkuList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList= storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                List<WcabCellInventoryProduct> skuInventoryList= inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                if (WcabCellInventoryInout.InoutType.out.name().equals(inOutTypeName)
//                        ||WcabCellInventoryInout.InoutType.unload.name().equals(inOutTypeName)
                ){
//                    if (spaceSkuList.size()==0){
//                        throw new BusinessException("未找到柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品");
//                    }else{

                    Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                    BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();


                    if (quantity>0){
                        quantity=-1*quantity;
                    }
                    if (weight.doubleValue()>0){
                        weight=weight.multiply(BigDecimal.valueOf(-1));
                    }
                    if (spaceInventoryList.size()>0){
                        wcabCellInventoryInoutDetailForm.setSkuCode(spaceSkuList.get(0).getSkuCode());
                        Integer currentQuantity=spaceSkuList.get(0).getQuantity();
                        BigDecimal currentWeight=spaceSkuList.get(0).getWeight();
                        Integer newQuantity=currentQuantity+quantity;
                        BigDecimal newWeight=currentWeight.add(weight);
                        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                            if (newQuantity<0){
//                                throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                            }
                        }else{
                            if (newWeight.doubleValue()<0){
//                                throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                            }
                        }

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceSkuList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);
                        wcabCellInventorySpace.setWeight(newWeight);
                        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                            if (newQuantity<=0){
                                inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                            }else{
                                inventorySpaceService.updateById(wcabCellInventorySpace);
                            }
                        }else{
                            if (newWeight.doubleValue()<=0){
                                inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                            }else{
                                inventorySpaceService.updateById(wcabCellInventorySpace);
                            }
                        }
                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                    }



                    if(skuSingleInventoryList.size()>0){
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                        wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                        wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                            if (wcabCellSingleInventoryProduct.getQuantity()<=0){
                                storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                            }else{
                                storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                            }
                        }else{
                            if (wcabCellSingleInventoryProduct.getWeight().doubleValue()<=0){
                                storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                            }else{
                                storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                            }
                        }

                    }

                    if(skuInventoryList.size()>0){
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                        wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                        wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                            if (wcabCellInventoryProduct.getQuantity()<=0){
                                inventoryProductService.removeById(skuInventoryList.get(0).getId());
                            }else{
                                inventoryProductService.updateById(wcabCellInventoryProduct);
                            }
                        }else{
                            if (wcabCellInventoryProduct.getWeight().doubleValue()<=0){
                                inventoryProductService.removeById(skuInventoryList.get(0).getId());
                            }else{
                                inventoryProductService.updateById(wcabCellInventoryProduct);
                            }
                        }

                    }


                    wcabCellInventoryInoutDetail.setWeight(weight);
                    wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.out);
                    baseMapper.insert(wcabCellInventoryInoutDetail);
//                    }
                }else if(WcabCellInventoryInout.InoutType.in.name().equals(inOutTypeName)){

                    JsonResult usableResult=isInventoryIn(wcabCellInventoryInoutDetailForm);
                    if (JsonResult.Code.SUCCESS.getCode()!=usableResult.getCode()){
                        throw new BusinessException(usableResult.getMsg());
                    }
                    Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                    if (quantity<=0){
                        throw new BusinessException("上货数量参数异常");
                    }
                    BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                    if (weight.doubleValue()<0){
                        throw new BusinessException("上货重量参数异常");
                    }
                    if (spaceInventoryList.size()==0){
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setQuantity(quantity);
                        wcabCellInventorySpace.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventorySpace.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());
                        wcabCellInventorySpace.setWeight(weight);
                        wcabCellInventorySpace.setEnterpriseId(enterpriseId);
                        wcabCellInventorySpace.setEnterpriseCode(enterpriseCode);
                        wcabCellInventorySpace.setBusinessType(WcabCellInventorySpace.BusinessType.jsonCreator(businessType.name()));
                        inventorySpaceService.save(wcabCellInventorySpace);

                        wcabCellInventoryInoutDetail.setQuantityCurrent(0);
                        wcabCellInventoryInoutDetail.setQuantityAfter(quantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setWeightAfter(weight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }else{
                        Integer currentQuantity=spaceInventoryList.get(0).getQuantity();
                        Integer newQuantity=currentQuantity+quantity;
                        BigDecimal currentWeight=spaceInventoryList.get(0).getWeight();
                        BigDecimal newWeight=currentWeight.add(weight);

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);
                        wcabCellInventorySpace.setWeight(newWeight);
//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                    if (skuSingleInventoryList.size()==0){
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(quantity);
                        wcabCellSingleInventoryProduct.setWeight(weight);
                        wcabCellSingleInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellSingleInventoryProduct.setBusinessType(WcabCellSingleInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        storageSingleInventoryProductService.save(wcabCellSingleInventoryProduct);
                    }else{
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                        wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                    }

                    if (skuInventoryList.size()==0){
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(quantity);
                        wcabCellInventoryProduct.setWeight(weight);
                        wcabCellInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellInventoryProduct.setBusinessType(WcabCellInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        inventoryProductService.save(wcabCellInventoryProduct);
                    }else{
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                        wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                        inventoryProductService.updateById(wcabCellInventoryProduct);
                    }
                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交领用物料数据");
        }
    }

    @Transactional
    void saveDetail4CheckQuantity(List<WcabCellInventoryInoutDetailForm> list,Long inventoryInoutId,String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,Boolean isCheck){
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isNotEmpty(list)){
            if (WcabUseInventoryInout.InoutType.in.name().equals(inOutTypeName)&&
                    !isCheck&&
                    (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                saveCheckData(list,businessType,inventoryInoutId,null);
            }
            for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabCellInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setEnterpriseId(enterpriseId);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
//                wcabCellInventoryInoutDetail.setType(WcabCellInventoryInoutDetail.Type.jsonCreator(inOutTypeName));
//                wcabCellInventoryInoutDetail.setbu
                wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.jsonCreator(inOutTypeName));
                wcabCellInventoryInoutDetail.setBusinessType(WcabCellInventoryInoutDetail.BusinessType.jsonCreator(businessType.name()));
                wcabCellInventoryInoutDetail.setInoutId(inventoryInoutId);
                List<CabinetCargoLane> cabinetCargoSpaces=laneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoSpaces.size()==0){
                    throw new BusinessException("未找到货道");
                }

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));

                if (WcabUseInventoryInout.Type.in.name().equals(inOutTypeName)){


                    log.info("{},{}",creator,cabinetCargoSpaces.get(0).getPrincipal());
                    if(cabinetCargoSpaces.get(0).getPrincipal()!=null&&cabinetCargoSpaces.get(0).getPrincipal()!=0){
                        if (!creator.equals(cabinetCargoSpaces.get(0).getPrincipal())){
                            throw new BusinessException("柜子编号["+cabinetCargoSpaces.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
                        }

                    }
                    if (StringUtils.isNotBlank(cabinetCargoSpaces.get(0).getSkuCode())){
                        if (!wcabCellInventoryInoutDetailForm.getSkuCode().equals(cabinetCargoSpaces.get(0).getSkuCode())){
                            throw new BusinessException("此库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]不能存储此产品SKU编号["+wcabCellInventoryInoutDetailForm.getSkuCode()+"]的产品，只能存放SKU编号为"+cabinetCargoSpaces.get(0).getSkuCode()+"的产品");
                        }
                    }
                    if (cabinetCargoSpaces.get(0).getSpaceProductType()!=null&&cabinetCargoSpaces.get(0).getSpaceProductType()!=0){
                        Map paramMap=new HashMap();
                        paramMap.put("skuCode4app",wcabCellInventoryInoutDetailForm.getSkuCode());
                        List<ProductDetailVO> productDetailVOS=productDetailService.list4app(paramMap);
                        if (productDetailVOS.size()==0){
                            throw new BusinessException("未找到产品");
                        }
                        if(!cabinetCargoSpaces.get(0).getSpaceProductType().equals(productDetailVOS.get(0).getProductTypeId())){
                            throw new BusinessException("此库位不能存储此产品类型["+productDetailVOS.get(0).getProductTypeName()+"]");
                        }

                    }
                    if (cabinetCargoSpaces.get(0).getSpaceType()!=null&&!CabinetCargoSpace.SpaceType.common.equals(cabinetCargoSpaces.get(0).getSpaceType())){
                        if (!cabinetCargoSpaces.get(0).getSpaceType().name().equals(businessType.name())
                        ){
                            throw new BusinessException("此库位只能存贮["+cabinetCargoSpaces.get(0).getSpaceType().getDesc()+"],不能再存贮["+businessType.getDesc()+"]");
                        }
                    }
                    if (spaceInventoryList.size()>0&&!spaceInventoryList.get(0).getBusinessType().name().equals(businessType.name())){
                        throw new BusinessException("此库位当前存贮的是["+spaceInventoryList.get(0).getBusinessType().getDesc()+"]产品,不能再存贮["+businessType.getDesc()+"]产品");
                    }
                    if (cabinetCargoSpaces.get(0).getMaxBearing()!=null){
                        if (CollectionUtil.isEmpty(spaceInventoryList)){
                            BigDecimal surplusWeight=cabinetCargoSpaces.get(0).getMaxBearing().subtract(wcabCellInventoryInoutDetailForm.getWeight());
                            if (surplusWeight.doubleValue()<0){
                                throw new BusinessException("上货重量["+wcabCellInventoryInoutDetailForm.getWeight()+"]超过此货道设置的最大上货重量["+cabinetCargoSpaces.get(0).getMaxBearing()+"]");
                            }
                        }
                        BigDecimal sum=getSumWeightBySpace(spaceInventoryList);
                        sum=sum.add(wcabCellInventoryInoutDetailForm.getWeight());
                        if (cabinetCargoSpaces.get(0).getMaxBearing().subtract(sum).doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"]库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]已经存满");
                        }
                    }
//                    List<WcabCellInventoryUser> skuInventoryUserList=cellInventoryUserService.list(new QueryWrapper<WcabCellInventoryUser>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
//                    if (
//                            (!spaceUserShareFlag&&!StorageInventoryInout.BusinessType.temp.equals(businessType))||
//                            (!spaceTempUserShareFlag&&StorageInventoryInout.BusinessType.temp.equals(businessType))&&
//                                    skuInventoryUserList.size()>0&&!creator.equals(skuInventoryUserList.get(0).getCreator())){
//                        throw new BusinessException("此货道已被他人占用");
//                    }
                }
                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                List<WcabCellInventorySpace> spaceSkuList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList= storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                List<WcabCellInventoryProduct> skuInventoryList= inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                if (WcabCellInventoryInout.InoutType.out.name().equals(inOutTypeName)
//                        ||WcabCellInventoryInout.InoutType.unload.name().equals(inOutTypeName)
                ){
                    if (spaceSkuList.size()==0){
                        throw new BusinessException("未找到柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品");
                    }else{
                        wcabCellInventoryInoutDetailForm.setSkuCode(spaceSkuList.get(0).getSkuCode());
                        Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                        BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                        if (quantity<=0){
                            throw new BusinessException("领取数量参数异常");
                        }

                        if (quantity>0){
                            quantity=-1*quantity;
                        }

                        Integer currentQuantity=spaceSkuList.get(0).getQuantity();

                        Integer newQuantity=currentQuantity+quantity;

                        if ((productDetailVO!=null&&ProductDetail.InventoryUnitType.quantity.equals(productDetailVO.getInventoryUnitType()))&&newQuantity<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                        }

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceSkuList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);

                            inventorySpaceService.updateById(wcabCellInventorySpace);



                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);


                                storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);



                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);

                                inventoryProductService.updateById(wcabCellInventoryProduct);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(skuInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeightAfter(skuInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.out);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                }else if(WcabCellInventoryInout.InoutType.in.name().equals(inOutTypeName)){


                    Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                    if (quantity<=0){
                        throw new BusinessException("上货数量参数异常");
                    }
                    if (spaceInventoryList.size()==0){
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setQuantity(quantity);
                        wcabCellInventorySpace.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventorySpace.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());

                        wcabCellInventorySpace.setEnterpriseId(enterpriseId);
                        wcabCellInventorySpace.setEnterpriseCode(enterpriseCode);
                        wcabCellInventorySpace.setBusinessType(WcabCellInventorySpace.BusinessType.jsonCreator(businessType.name()));
                        inventorySpaceService.save(wcabCellInventorySpace);

                        wcabCellInventoryInoutDetail.setQuantityCurrent(0);
                        wcabCellInventoryInoutDetail.setQuantityAfter(quantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setWeightAfter(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setWeight(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }else{
                        Integer currentQuantity=spaceInventoryList.get(0).getQuantity();
                        Integer newQuantity=currentQuantity+quantity;



                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);

//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeightAfter(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeight(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                    if (skuSingleInventoryList.size()==0){
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(quantity);
                        wcabCellSingleInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellSingleInventoryProduct.setBusinessType(WcabCellSingleInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        storageSingleInventoryProductService.save(wcabCellSingleInventoryProduct);
                    }else{
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                    }

                    if (skuInventoryList.size()==0){
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(quantity);
                        wcabCellInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellInventoryProduct.setBusinessType(WcabCellInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        inventoryProductService.save(wcabCellInventoryProduct);
                    }else{
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                        inventoryProductService.updateById(wcabCellInventoryProduct);
                    }
                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交领用物料数据");
        }
    }

    @Transactional
    void saveDetail4CheckWeight(List<WcabCellInventoryInoutDetailForm> list,Long inventoryInoutId,String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,Boolean isCheck){
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isNotEmpty(list)){
            if (WcabUseInventoryInout.InoutType.in.name().equals(inOutTypeName)&&
                    !isCheck&&
                    (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                saveCheckData(list,businessType,inventoryInoutId,null);
            }
            for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabCellInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setEnterpriseId(enterpriseId);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
//                wcabCellInventoryInoutDetail.setType(WcabCellInventoryInoutDetail.Type.jsonCreator(inOutTypeName));
//                wcabCellInventoryInoutDetail.setbu
                wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.jsonCreator(inOutTypeName));
                wcabCellInventoryInoutDetail.setBusinessType(WcabCellInventoryInoutDetail.BusinessType.jsonCreator(businessType.name()));
                wcabCellInventoryInoutDetail.setInoutId(inventoryInoutId);
                List<CabinetCargoLane> cabinetCargoSpaces=laneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoSpaces.size()==0){
                    throw new BusinessException("未找到货道");
                }

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));

                if (WcabUseInventoryInout.Type.in.name().equals(inOutTypeName)){


                    log.info("{},{}",creator,cabinetCargoSpaces.get(0).getPrincipal());
                    if(cabinetCargoSpaces.get(0).getPrincipal()!=null&&cabinetCargoSpaces.get(0).getPrincipal()!=0){
                        if (!creator.equals(cabinetCargoSpaces.get(0).getPrincipal())){
                            throw new BusinessException("柜子编号["+cabinetCargoSpaces.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
                        }

                    }
                    if (StringUtils.isNotBlank(cabinetCargoSpaces.get(0).getSkuCode())){
                        if (!wcabCellInventoryInoutDetailForm.getSkuCode().equals(cabinetCargoSpaces.get(0).getSkuCode())){
                            throw new BusinessException("此库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]不能存储此产品SKU编号["+wcabCellInventoryInoutDetailForm.getSkuCode()+"]的产品，只能存放SKU编号为"+cabinetCargoSpaces.get(0).getSkuCode()+"的产品");
                        }
                    }
                    if (cabinetCargoSpaces.get(0).getSpaceProductType()!=null&&cabinetCargoSpaces.get(0).getSpaceProductType()!=0){
                        Map paramMap=new HashMap();
                        paramMap.put("skuCode4app",wcabCellInventoryInoutDetailForm.getSkuCode());
                        List<ProductDetailVO> productDetailVOS=productDetailService.list4app(paramMap);
                        if (productDetailVOS.size()==0){
                            throw new BusinessException("未找到产品");
                        }
                        if(!cabinetCargoSpaces.get(0).getSpaceProductType().equals(productDetailVOS.get(0).getProductTypeId())){
                            throw new BusinessException("此库位不能存储此产品类型["+productDetailVOS.get(0).getProductTypeName()+"]");
                        }

                    }
                    if (cabinetCargoSpaces.get(0).getSpaceType()!=null&&!CabinetCargoSpace.SpaceType.common.equals(cabinetCargoSpaces.get(0).getSpaceType())){
                        if (!cabinetCargoSpaces.get(0).getSpaceType().name().equals(businessType.name())
                        ){
                            throw new BusinessException("此库位只能存贮["+cabinetCargoSpaces.get(0).getSpaceType().getDesc()+"],不能再存贮["+businessType.getDesc()+"]");
                        }
                    }
                    if (spaceInventoryList.size()>0&&!spaceInventoryList.get(0).getBusinessType().name().equals(businessType.name())){
                        throw new BusinessException("此库位当前存贮的是["+spaceInventoryList.get(0).getBusinessType().getDesc()+"]产品,不能再存贮["+businessType.getDesc()+"]产品");
                    }
                    if (cabinetCargoSpaces.get(0).getMaxBearing()!=null){
                        if (CollectionUtil.isEmpty(spaceInventoryList)){
                            BigDecimal surplusWeight=cabinetCargoSpaces.get(0).getMaxBearing().subtract(wcabCellInventoryInoutDetailForm.getWeight());
                            if (surplusWeight.doubleValue()<0){
                                throw new BusinessException("上货重量["+wcabCellInventoryInoutDetailForm.getWeight()+"]超过此货道设置的最大上货重量["+cabinetCargoSpaces.get(0).getMaxBearing()+"]");
                            }
                        }
                        BigDecimal sum=getSumWeightBySpace(spaceInventoryList);
                        sum=sum.add(wcabCellInventoryInoutDetailForm.getWeight());
                        if (cabinetCargoSpaces.get(0).getMaxBearing().subtract(sum).doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"]库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]已经存满");
                        }
                    }
//                    List<WcabCellInventoryUser> skuInventoryUserList=cellInventoryUserService.list(new QueryWrapper<WcabCellInventoryUser>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
//                    if (
//                            (!spaceUserShareFlag&&!StorageInventoryInout.BusinessType.temp.equals(businessType))||
//                            (!spaceTempUserShareFlag&&StorageInventoryInout.BusinessType.temp.equals(businessType))&&
//                                    skuInventoryUserList.size()>0&&!creator.equals(skuInventoryUserList.get(0).getCreator())){
//                        throw new BusinessException("此货道已被他人占用");
//                    }
                }
                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                List<WcabCellInventorySpace> spaceSkuList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList= storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                List<WcabCellInventoryProduct> skuInventoryList= inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                if (WcabCellInventoryInout.InoutType.out.name().equals(inOutTypeName)
//                        ||WcabCellInventoryInout.InoutType.unload.name().equals(inOutTypeName)
                ){
                    if (spaceSkuList.size()==0){
                        throw new BusinessException("未找到柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品");
                    }else{
                        wcabCellInventoryInoutDetailForm.setSkuCode(spaceSkuList.get(0).getSkuCode());

                        BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();

                        if (weight.doubleValue()<=0){
                            throw new BusinessException("领取重量参数异常");
                        }

                        if (weight.doubleValue()>0){
                            weight=weight.multiply(BigDecimal.valueOf(-1));
                        }


                        BigDecimal currentWeight=spaceSkuList.get(0).getWeight();

                        BigDecimal newWeight=currentWeight.add(weight);

                        if ((productDetailVO!=null&&ProductDetail.InventoryUnitType.weight.equals(productDetailVO.getInventoryUnitType()))&&newWeight.doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                        }
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceSkuList.get(0).getId());

                        wcabCellInventorySpace.setWeight(newWeight);
                        if (newWeight.doubleValue()==0){
                            inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                        }else{
                            inventorySpaceService.updateById(wcabCellInventorySpace);
                        }

                        if(skuSingleInventoryList.size()>0){
                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                            if (wcabCellSingleInventoryProduct.getWeight().doubleValue()==0){
                                storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                            }else{
                                storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                            }
                        }

                        if(skuInventoryList.size()>0){
                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                            if (wcabCellInventoryProduct.getWeight().doubleValue()==0){
                                inventoryProductService.removeById(skuInventoryList.get(0).getId());
                            }else{
                                inventoryProductService.updateById(wcabCellInventoryProduct);
                            }
                        }

                        wcabCellInventoryInoutDetail.setQuantityCurrent(spaceSkuList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantityAfter(spaceSkuList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantity(spaceSkuList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.out);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                }else if(WcabCellInventoryInout.InoutType.in.name().equals(inOutTypeName)){


                    BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                    if (weight.doubleValue()<0){
                        throw new BusinessException("上货重量参数异常");
                    }
                    if (spaceInventoryList.size()==0){
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();

                        wcabCellInventorySpace.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventorySpace.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());
                        wcabCellInventorySpace.setWeight(weight);
                        wcabCellInventorySpace.setEnterpriseId(enterpriseId);
                        wcabCellInventorySpace.setEnterpriseCode(enterpriseCode);
                        wcabCellInventorySpace.setBusinessType(WcabCellInventorySpace.BusinessType.jsonCreator(businessType.name()));
                        inventorySpaceService.save(wcabCellInventorySpace);

                        wcabCellInventoryInoutDetail.setQuantityCurrent(0);
                        wcabCellInventoryInoutDetail.setQuantityAfter(0);
                        wcabCellInventoryInoutDetail.setQuantity(0);
                        wcabCellInventoryInoutDetail.setWeightCurrent(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setWeightAfter(weight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }else{


                        BigDecimal currentWeight=spaceInventoryList.get(0).getWeight();
                        BigDecimal newWeight=currentWeight.add(weight);

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());

                        wcabCellInventorySpace.setWeight(newWeight);
//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantityAfter(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantity(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                    if (skuSingleInventoryList.size()==0){
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());

                        wcabCellSingleInventoryProduct.setWeight(weight);
                        wcabCellSingleInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellSingleInventoryProduct.setBusinessType(WcabCellSingleInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        storageSingleInventoryProductService.save(wcabCellSingleInventoryProduct);
                    }else{
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());

                        wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                    }

                    if (skuInventoryList.size()==0){
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());

                        wcabCellInventoryProduct.setWeight(weight);
                        wcabCellInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellInventoryProduct.setBusinessType(WcabCellInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        inventoryProductService.save(wcabCellInventoryProduct);
                    }else{
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());

                        wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                        inventoryProductService.updateById(wcabCellInventoryProduct);
                    }
                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交领用物料数据");
        }
    }

    @Transactional
    void saveDetail4CheckQuantityWeight(List<WcabCellInventoryInoutDetailForm> list,Long inventoryInoutId,String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,Boolean isCheck){
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isNotEmpty(list)){
            if (WcabUseInventoryInout.InoutType.in.name().equals(inOutTypeName)&&
                    !isCheck&&
                    (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                saveCheckData(list,businessType,inventoryInoutId,null);
            }
            for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabCellInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setEnterpriseId(enterpriseId);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
//                wcabCellInventoryInoutDetail.setType(WcabCellInventoryInoutDetail.Type.jsonCreator(inOutTypeName));
//                wcabCellInventoryInoutDetail.setbu
                wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.jsonCreator(inOutTypeName));
                wcabCellInventoryInoutDetail.setBusinessType(WcabCellInventoryInoutDetail.BusinessType.jsonCreator(businessType.name()));

                wcabCellInventoryInoutDetail.setInoutId(inventoryInoutId);
                List<CabinetCargoLane> cabinetCargoSpaces=laneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoSpaces.size()==0){
                    throw new BusinessException("未找到货道");
                }

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));

                if (WcabUseInventoryInout.Type.in.name().equals(inOutTypeName)){


                    log.info("{},{}",creator,cabinetCargoSpaces.get(0).getPrincipal());
                    if(cabinetCargoSpaces.get(0).getPrincipal()!=null&&cabinetCargoSpaces.get(0).getPrincipal()!=0){
                        if (!creator.equals(cabinetCargoSpaces.get(0).getPrincipal())){
                            throw new BusinessException("柜子编号["+cabinetCargoSpaces.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
                        }

                    }
                    if (StringUtils.isNotBlank(cabinetCargoSpaces.get(0).getSkuCode())){
                        if (!wcabCellInventoryInoutDetailForm.getSkuCode().equals(cabinetCargoSpaces.get(0).getSkuCode())){
                            throw new BusinessException("此库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]不能存储此产品SKU编号["+wcabCellInventoryInoutDetailForm.getSkuCode()+"]的产品，只能存放SKU编号为"+cabinetCargoSpaces.get(0).getSkuCode()+"的产品");
                        }
                    }
                    if (cabinetCargoSpaces.get(0).getSpaceProductType()!=null&&cabinetCargoSpaces.get(0).getSpaceProductType()!=0){
                        Map paramMap=new HashMap();
                        paramMap.put("skuCode4app",wcabCellInventoryInoutDetailForm.getSkuCode());
                        List<ProductDetailVO> productDetailVOS=productDetailService.list4app(paramMap);
                        if (productDetailVOS.size()==0){
                            throw new BusinessException("未找到产品");
                        }
                        if(!cabinetCargoSpaces.get(0).getSpaceProductType().equals(productDetailVOS.get(0).getProductTypeId())){
                            throw new BusinessException("此库位不能存储此产品类型["+productDetailVOS.get(0).getProductTypeName()+"]");
                        }

                    }
                    if (cabinetCargoSpaces.get(0).getSpaceType()!=null&&!CabinetCargoSpace.SpaceType.common.equals(cabinetCargoSpaces.get(0).getSpaceType())){
                        if (!cabinetCargoSpaces.get(0).getSpaceType().name().equals(businessType.name())
                        ){
                            throw new BusinessException("此库位只能存贮["+cabinetCargoSpaces.get(0).getSpaceType().getDesc()+"],不能再存贮["+businessType.getDesc()+"]");
                        }
                    }
                    if (spaceInventoryList.size()>0&&!spaceInventoryList.get(0).getBusinessType().name().equals(businessType.name())){
                        throw new BusinessException("此库位当前存贮的是["+spaceInventoryList.get(0).getBusinessType().getDesc()+"]产品,不能再存贮["+businessType.getDesc()+"]产品");
                    }
                    if (cabinetCargoSpaces.get(0).getMaxBearing()!=null){
                        if (CollectionUtil.isEmpty(spaceInventoryList)){
                            BigDecimal surplusWeight=cabinetCargoSpaces.get(0).getMaxBearing().subtract(wcabCellInventoryInoutDetailForm.getWeight());
                            if (surplusWeight.doubleValue()<0){
                                throw new BusinessException("上货重量["+wcabCellInventoryInoutDetailForm.getWeight()+"]超过此货道设置的最大上货重量["+cabinetCargoSpaces.get(0).getMaxBearing()+"]");
                            }
                        }
                        BigDecimal sum=getSumWeightBySpace(spaceInventoryList);
                        sum=sum.add(wcabCellInventoryInoutDetailForm.getWeight());
                        if (cabinetCargoSpaces.get(0).getMaxBearing().subtract(sum).doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"]库位["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]已经存满");
                        }
                    }
//                    List<WcabCellInventoryUser> skuInventoryUserList=cellInventoryUserService.list(new QueryWrapper<WcabCellInventoryUser>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()));
//                    if (
//                            (!spaceUserShareFlag&&!StorageInventoryInout.BusinessType.temp.equals(businessType))||
//                            (!spaceTempUserShareFlag&&StorageInventoryInout.BusinessType.temp.equals(businessType))&&
//                                    skuInventoryUserList.size()>0&&!creator.equals(skuInventoryUserList.get(0).getCreator())){
//                        throw new BusinessException("此货道已被他人占用");
//                    }
                }
                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                List<WcabCellInventorySpace> spaceSkuList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList= storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                List<WcabCellInventoryProduct> skuInventoryList= inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabCellInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
                if (WcabCellInventoryInout.InoutType.out.name().equals(inOutTypeName)
//                        ||WcabCellInventoryInout.InoutType.unload.name().equals(inOutTypeName)
                ){
                    if (spaceSkuList.size()==0){
                        throw new BusinessException("未找到柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品");
                    }else{
                        wcabCellInventoryInoutDetailForm.setSkuCode(spaceSkuList.get(0).getSkuCode());
                        Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                        BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                        if (quantity<=0){
                            throw new BusinessException("领取数量参数异常");
                        }
                        if (weight.doubleValue()<=0){
                            throw new BusinessException("领取重量参数异常");
                        }
                        if (quantity>0){
                            quantity=-1*quantity;
                        }
                        if (weight.doubleValue()>0){
                            weight=weight.multiply(BigDecimal.valueOf(-1));
                        }

                        Integer currentQuantity=spaceSkuList.get(0).getQuantity();
                        BigDecimal currentWeight=spaceSkuList.get(0).getWeight();
                        Integer newQuantity=currentQuantity+quantity;
                        BigDecimal newWeight=currentWeight.add(weight);
                        if ((productDetailVO!=null&&ProductDetail.InventoryUnitType.quantity.equals(productDetailVO.getInventoryUnitType()))&&newQuantity<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                        }
                        if ((productDetailVO!=null&&ProductDetail.InventoryUnitType.weight.equals(productDetailVO.getInventoryUnitType()))&&newWeight.doubleValue()<0){
                            throw new BusinessException("柜子编号["+wcabCellInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabCellInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                        }
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceSkuList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);
                        wcabCellInventorySpace.setWeight(newWeight);
                        if (newWeight.doubleValue()==0){
                            inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                        }else{
                            inventorySpaceService.updateById(wcabCellInventorySpace);
                        }

                        if(skuSingleInventoryList.size()>0){
                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                            wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                            if (wcabCellSingleInventoryProduct.getWeight().doubleValue()==0){
                                storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                            }else{
                                storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                            }
                        }

                        if(skuInventoryList.size()>0){
                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                            wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                            if (wcabCellInventoryProduct.getWeight().doubleValue()==0){
                                inventoryProductService.removeById(skuInventoryList.get(0).getId());
                            }else{
                                inventoryProductService.updateById(wcabCellInventoryProduct);
                            }
                        }

                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.out);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                }else if(WcabCellInventoryInout.InoutType.in.name().equals(inOutTypeName)){


                    Integer quantity=wcabCellInventoryInoutDetailForm.getQuantity();
                    if (quantity<=0){
                        throw new BusinessException("上货数量参数异常");
                    }
                    BigDecimal weight=wcabCellInventoryInoutDetailForm.getWeight();
                    if (weight.doubleValue()<0){
                        throw new BusinessException("上货重量参数异常");
                    }
                    if (spaceInventoryList.size()==0){
                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setQuantity(quantity);
                        wcabCellInventorySpace.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventorySpace.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());
                        wcabCellInventorySpace.setWeight(weight);
                        wcabCellInventorySpace.setEnterpriseId(enterpriseId);
                        wcabCellInventorySpace.setEnterpriseCode(enterpriseCode);
                        wcabCellInventorySpace.setBusinessType(WcabCellInventorySpace.BusinessType.jsonCreator(businessType.name()));
                        inventorySpaceService.save(wcabCellInventorySpace);

                        wcabCellInventoryInoutDetail.setQuantityCurrent(0);
                        wcabCellInventoryInoutDetail.setQuantityAfter(quantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(BigDecimal.valueOf(0));
                        wcabCellInventoryInoutDetail.setWeightAfter(weight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }else{
                        Integer currentQuantity=spaceInventoryList.get(0).getQuantity();
                        Integer newQuantity=currentQuantity+quantity;
                        BigDecimal currentWeight=spaceInventoryList.get(0).getWeight();
                        BigDecimal newWeight=currentWeight.add(weight);

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);
                        wcabCellInventorySpace.setWeight(newWeight);
//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
                    }
                    if (skuSingleInventoryList.size()==0){
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(quantity);
                        wcabCellSingleInventoryProduct.setWeight(weight);
                        wcabCellSingleInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellSingleInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellSingleInventoryProduct.setBusinessType(WcabCellSingleInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        storageSingleInventoryProductService.save(wcabCellSingleInventoryProduct);
                    }else{
                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                        wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                    }

                    if (skuInventoryList.size()==0){
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(quantity);
                        wcabCellInventoryProduct.setWeight(weight);
                        wcabCellInventoryProduct.setEnterpriseId(enterpriseId);
                        wcabCellInventoryProduct.setEnterpriseCode(enterpriseCode);
                        wcabCellInventoryProduct.setBusinessType(WcabCellInventoryProduct.BusinessType.jsonCreator(businessType.name()));
                        inventoryProductService.save(wcabCellInventoryProduct);
                    }else{
                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                        wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                        inventoryProductService.updateById(wcabCellInventoryProduct);
                    }
                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交领用物料数据");
        }
    }

    @Override
    @Transactional
    public boolean check4Wx(WcabCellInventoryInoutForm storageInventoryInoutForm) throws IOException {
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        WcabCellInventoryInout.BusinessType businessType=storageInventoryInoutForm.getBusinessType();
        String cabCode=storageInventoryInoutForm.getDetailList().get(0).getCabCode();
        String spaceCode=storageInventoryInoutForm.getDetailList().get(0).getSpaceCode();
        String skuCode=storageInventoryInoutForm.getDetailList().get(0).getSkuCode();
        Map params=new HashMap();
        params.put("cabCode",cabCode);
        params.put("spaceCode",spaceCode);
        List<WcabCellInventorySpaceVO> inventorySpaces= inventorySpaceService.list4app(params);
        if (inventorySpaces.size()==0){
            throw new BusinessException("未找到此货位上的库存数据");
        }
        String businessTypePre=inventorySpaces.get(0).getBusinessType().name();
        if (businessType==null){
            businessType=WcabCellInventoryInout.BusinessType.jsonCreator(businessTypePre);
        }
//        CabinetOpenServer.sendMessage(cabCode, spaceCode);

        if (WcabCellInventoryInout.BusinessType.normal.equals(businessType)&&!WcabCellInventoryInout.BusinessType.normal.name().equals(businessTypePre)){
            if (inventorySpaces.size()==1&&
                    (CabinetCargoLane.SpaceType.normal.equals(inventorySpaces.get(0).getSpaceType())||CabinetCargoLane.SpaceType.common.equals(inventorySpaces.get(0).getSpaceType()))){
                storageInventoryInoutForm.getDetailList().forEach(item->{
                    item.setTargetCabCode(cabCode);
                    item.setTargetSpaceCode(spaceCode);
                });
            }else{
                List<Cabinet> cabinets=cabinetService.list(new QueryWrapper<Cabinet>().eq("code",cabCode));
                if (cabinets.size()==0){
                    throw new BusinessException("未找到柜子编号["+cabCode+"]的柜子");
                }
                UseInventoryInoutDetailForm useInventoryInoutDetailForm=new UseInventoryInoutDetailForm();
                useInventoryInoutDetailForm.setStorageType(CabinetCargoSpace.SpaceNature.common);
                useInventoryInoutDetailForm.setSkuCode(skuCode);
                cabinets=cabinetService.list(new QueryWrapper<Cabinet>().eq("cabinet_type",Cabinet.CabinetType.gravity.getValue()).eq("belong_cabinet",cabinets.get(0).getBelongCabinet()));
                String cabCodes="";
                for (Cabinet cabinet:cabinets){
                    cabCodes+=cabinet.getCode()+",";
                }
                useInventoryInoutDetailForm.setCabCode(cabCodes.substring(0,cabCodes.length()-1));
                useInventoryInoutDetailForm.setQuantity(1);
//                final CabinetCargoSpaceVO spaceVO = this.getUsableSpaces(useInventoryInoutDetailForm, StorageInventoryInout.BusinessType.temp);
                List<Integer> spaceTypes=new ArrayList<>();
                spaceTypes.add(CabinetCargoLane.SpaceType.common.getValue());
                spaceTypes.add(CabinetCargoLane.SpaceType.normal.getValue());
                List<CabinetCargoLaneVO> spaceVOs = this.getSpaces(useInventoryInoutDetailForm.getCabCode(),useInventoryInoutDetailForm.getSkuCode(), WcabCellInventoryInout.BusinessType.normal,spaceTypes,null);
                if (spaceVOs.size()==0){
                    throw new BusinessException("未找到可用的货道");
                }
                final CabinetCargoLaneVO spaceVO =spaceVOs.get(0) ;
                log.info("space data:{}", JSONObject.toJSONString(spaceVO));
//                if (spaceVO == null) {
//                    if(CabinetCargoSpace.SpaceNature.personal.equals(useInventoryInoutDetailForm.getStorageType())){
//                        throw new BusinessException("未找到可用的专属库位,请先进行库位设置");
//                    }else if(CabinetCargoSpace.SpaceNature.specialClass.equals(useInventoryInoutDetailForm.getStorageType())){
//                        throw new BusinessException("未找到可用的专类库位，请先进行库位设置");
//                    }else if(CabinetCargoSpace.SpaceNature.common.equals(useInventoryInoutDetailForm.getStorageType())){
//                        throw new BusinessException("未找到可用的公共库位");
//                    }else{
//                        throw new BusinessException("存贮性质参数错误");
//                    }
//                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("cabCode",spaceVO.getCabCode());
                jsonObject.put("spaceCode",spaceCode);
                jsonObject.put("type","remoteOpen");
                Cabinet cabinet = cabinetService.getCabinetVOByCabCode(spaceVO.getCabCode());

                CabinetOpenServer.sendMessage(enterpriseCode,cabinet.getBelongCabinet(), jsonObject.toJSONString());
                storageInventoryInoutForm.getDetailList().forEach(item->{
                    item.setTargetCabCode(spaceVO.getCabCode());
                    item.setTargetSpaceCode(spaceVO.getCode());
                });
            }

        }

        check(storageInventoryInoutForm);
        return true;
    }

    @Override
    @Transactional
    public boolean check(WcabCellInventoryInoutForm wcabCellInventoryInoutForm) {
        if (CollectionUtil.isEmpty(wcabCellInventoryInoutForm.getDetailList())){
            throw new BusinessException("缺少盘验明细参数");
        }
        if (wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
            for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:wcabCellInventoryInoutForm.getDetailList()){
                if(StringUtils.isBlank(wcabCellInventoryInoutDetailForm.getSkuCodeChecked())){
                    throw new BusinessException("产品不符必须选择产品");
                }
            }
        }
        String remark="";
        if (wcabCellInventoryInoutForm.getFlag()==null||wcabCellInventoryInoutForm.getFlag()==false){
            wcabCellInventoryInoutForm.setIsCorrect(false);
        }
        if (wcabCellInventoryInoutForm.getFlag()!=null&&wcabCellInventoryInoutForm.getFlag()){
            wcabCellInventoryInoutForm.setIsCorrect(true);
            wcabCellInventoryInoutForm.setBusinessTypeCorrect(true);
            wcabCellInventoryInoutForm.setSkuCorrect(true);
            remark="相符";
        }else{
            wcabCellInventoryInoutForm.setIsCorrect(false);
        }
        if (wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
            wcabCellInventoryInoutForm.setIsCorrect(true);
            wcabCellInventoryInoutForm.setSkuCorrect(false);
            wcabCellInventoryInoutForm.setBusinessTypeCorrect(true);
            remark="规格不符;";
        }else{
            wcabCellInventoryInoutForm.setSkuCorrect(true);
        }
        if (wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag()){
            wcabCellInventoryInoutForm.setIsCorrect(false);
            wcabCellInventoryInoutForm.setWeightCorrect(false);
            remark+="重量不符;";
        }else{
            wcabCellInventoryInoutForm.setWeightCorrect(true);
        }
        if (wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag()){
            wcabCellInventoryInoutForm.setIsCorrect(false);
            wcabCellInventoryInoutForm.setQuantityCorrect(false);
            remark+="数量不符;";
        }else{
            wcabCellInventoryInoutForm.setQuantityCorrect(true);
        }
        if (wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
            wcabCellInventoryInoutForm.setIsCorrect(false);
            remark="SKU不符["+wcabCellInventoryInoutForm.getDetailList().get(0).getSkuCode()+"->"+wcabCellInventoryInoutForm.getDetailList().get(0).getSkuCodeChecked()+"];";
        }
        wcabCellInventoryInoutForm.getDetailList().get(0).setIsCorrect(wcabCellInventoryInoutForm.getIsCorrect());
//        wcabCellInventoryInoutForm.getDetailList().get(0).setBusinessTypeCorrect(wcabCellInventoryInoutForm.getBusinessTypeCorrect());
        wcabCellInventoryInoutForm.setBusinessTypeCorrect(true);
        wcabCellInventoryInoutForm.getDetailList().get(0).setSkuCorrect(wcabCellInventoryInoutForm.getSkuCorrect());
        wcabCellInventoryInoutForm.getDetailList().get(0).setWeightCorrect(wcabCellInventoryInoutForm.getWeightCorrect());
        wcabCellInventoryInoutForm.getDetailList().get(0).setQuantityCorrect(wcabCellInventoryInoutForm.getQuantityCorrect());

//        WcabCellInventoryInout.BusinessType businessType=wcabCellInventoryInoutForm.getBusinessType();
//        CabinetCargoSpaceVO cabinetCargoSpaceVO=cargoSpaceService.get4appById(spaceId);
//        if (cabinetCargoSpaceVO==null) {
//            throw new BusinessException("货位未找到");
//        }
//        Map params=new HashMap();
//        params.put("cabCode",cabinetCargoSpaceVO.getCabCode());
//        params.put("spaceCode",cabinetCargoSpaceVO.getCode());
//        List<StorageInventoryUncheckVO> list=inventoryUncheckService.list4app(params);
        String cabCode=wcabCellInventoryInoutForm.getDetailList().get(0).getCabCode();
        String spaceCode=wcabCellInventoryInoutForm.getDetailList().get(0).getSpaceCode();
//        List<WcabCellInventoryInoutDetailForm> inventoryList4Combine=combine(wcabCellInventoryInoutForm.getDetailList());
        List<WcabCellInventoryInoutDetailForm> inventoryList=wcabCellInventoryInoutForm.getDetailList();
        List<WcabCellInventorySpace> inventorySpaces= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",cabCode).eq("space_code",spaceCode));

//        if ((!StorageInventoryInout.BusinessType.temp.equals(businessType)&&list.size()==0)){
//            throw new BusinessException("未找到盘验任务数据");
//        }
        if (inventorySpaces.size()==0){
            throw new BusinessException("未找到此货位上的库存数据");
        }
        Map<String,WcabCellInventorySpace> quantityMap=new HashMap<>();
        inventorySpaces.forEach(e->{
            quantityMap.put(e.getSkuCode(),e);
        });
        Map<String,WcabCellInventoryInoutDetailForm> quantityMap2=new HashMap<>();
        wcabCellInventoryInoutForm.getDetailList().forEach(e->{

                WcabCellInventorySpace wcabCellInventorySpace=quantityMap.get(e.getSkuCode());
                if (wcabCellInventorySpace!=null){
                    if (e.getQuantity()==null){
                        e.setQuantity(wcabCellInventorySpace.getQuantity());
                        e.setQuantityChecked(wcabCellInventorySpace.getQuantity());
                    }
                    if (e.getWeight()==null){
                        e.setWeight(wcabCellInventorySpace.getWeight());
                        e.setWeightChecked(wcabCellInventorySpace.getWeight());

                    }
                    if (WcabCellInventoryInout.BusinessType.losed.equals(wcabCellInventoryInoutForm.getBusinessType())){
                        e.setQuantity(wcabCellInventorySpace.getQuantity());
                        e.setQuantityChecked(0);
                        e.setWeight(wcabCellInventorySpace.getWeight());
                        e.setWeightChecked(BigDecimal.valueOf(0));
                    }
                    if (wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
//                        e.setQuantityChecked(wcabCellInventorySpace.getQuantity());
//                        e.setWeightChecked(wcabCellInventorySpace.getWeight());
//                        e.setQuantity(wcabCellInventorySpace.getQuantity());
//                        e.setWeight(wcabCellInventorySpace.getWeight());
                    }else{
                        e.setSkuCodeChecked(e.getSkuCode());
                    }

                    if (wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag()){
                        e.setWeightChecked(e.getWeight());
//                        e.setQuantity(wcabCellInventorySpace.getQuantity());
                        e.setWeight(wcabCellInventorySpace.getWeight());
//                        if (wcabCellInventoryInoutForm.getQuantityFlag()==null||
//                                (wcabCellInventoryInoutForm.getQuantityFlag()!=null&&!wcabCellInventoryInoutForm.getQuantityFlag())){
//                            e.setQuantityChecked(wcabCellInventorySpace.getQuantity());
//                        }
                    }else{
                        e.setWeightChecked(wcabCellInventorySpace.getWeight());
                        e.setWeight(wcabCellInventorySpace.getWeight());
                    }
                    if (wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag()){
                        e.setQuantityChecked(e.getQuantity());
                        e.setQuantity(wcabCellInventorySpace.getQuantity());
//                        e.setWeight(wcabCellInventorySpace.getWeight());
//                        if (wcabCellInventoryInoutForm.getWeightFlag()==null||
//                                (wcabCellInventoryInoutForm.getWeightFlag()!=null&&!wcabCellInventoryInoutForm.getWeightFlag())){
//                            e.setWeightChecked(wcabCellInventorySpace.getWeight());
//                        }

                    }else{
                        e.setQuantityChecked(wcabCellInventorySpace.getQuantity());
                        e.setQuantity(wcabCellInventorySpace.getQuantity());
                    }
                    e.setQuantityPre(wcabCellInventorySpace.getQuantity());
                    e.setWeightPre(wcabCellInventorySpace.getWeight());

                    if (wcabCellInventoryInoutForm.getFlag()!=null&&wcabCellInventoryInoutForm.getFlag()){
                        e.setQuantityChecked(wcabCellInventorySpace.getQuantity());
                        e.setWeightChecked(wcabCellInventorySpace.getWeight());
                        e.setQuantity(wcabCellInventorySpace.getQuantity());
                        e.setWeight(wcabCellInventorySpace.getWeight());
                        e.setSkuCodeChecked(e.getSkuCode());
                    }
                    quantityMap2.put(e.getSkuCode(),e);


                }

        });

//
        WcabCellInventoryInout.BusinessType businessType=wcabCellInventoryInoutForm.getBusinessType();
        if (businessType==null){
            businessType=WcabCellInventoryInout.BusinessType.jsonCreator(inventorySpaces.get(0).getBusinessType().name());
            wcabCellInventoryInoutForm.setBusinessType(businessType);
        }
        List<StorageInventoryInoutDetail> checkList=new ArrayList<>();
        String businessTypeChanged=businessType.name();
        String businessTypePre=inventorySpaces.get(0).getBusinessType().name();

        if (businessTypeChanged==null){
            businessTypeChanged=businessTypePre;
        }
        log.info("{}",WcabCellInventoryInout.BusinessType.normal.equals(businessType));
        log.info("{}",WcabCellInventorySpace.BusinessType.normal.equals(inventorySpaces.get(0).getBusinessType()));
        if (wcabCellInventoryInoutForm.getFlag()!=null&&wcabCellInventoryInoutForm.getFlag()&&WcabCellInventoryInout.BusinessType.normal.equals(businessType)){
//            StorageInventoryInoutDetail storageInventoryInoutDetail=new StorageInventoryInoutDetail();
//            BeanUtils.copyProperties(useInventoryInoutDetailForm,storageInventoryInoutDetail);
//            saveCheckedResult(storageInventoryInoutDetail,businessType, businessType.name(),storageInventorySpace.getQuantity());
            saveCheckedResult(wcabCellInventoryInoutForm,businessTypePre,getQuantity(inventorySpaces),getCheckWeight(wcabCellInventoryInoutForm.getDetailList()));
            return true;
        }
        if (!businessTypeChanged.equals(businessTypePre)){
            remark="状态不符";
        }
        if (WcabCellInventoryInout.BusinessType.losed.equals(wcabCellInventoryInoutForm.getBusinessType())){
            remark="缺失";
        }
        wcabCellInventoryInoutForm.setRemark(remark);
        wcabCellInventoryInoutForm.getDetailList().get(0).setRemark(remark);

//        Long checkedResultId=saveCheckedResult(checkList,businessType,businessTypePre,inventorySpaces.get(0).getQuantity());
        Long checkedResultId=saveCheckedResult(wcabCellInventoryInoutForm,businessTypePre,getQuantity(inventorySpaces),getCheckWeight(wcabCellInventoryInoutForm.getDetailList()));

//        List<StorageInventoryInoutDetailForm> inventoryList=new ArrayList<>();

//        inventoryList.add(useInventoryInoutDetailForm);

        if (WcabCellInventoryInout.BusinessType.normal.name().equals(businessTypePre)){
            /**
            if (WcabCellInventoryInout.BusinessType.repair.name().equals(businessTypeChanged)){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId, WcabCheckedInventoryInout.BusinessType.repair,WcabCheckedInventoryInout.BusinessTypePre.normal);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.repair,checkedInventoryInout.getId());
//                checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId, CheckedInventoryInout.BusinessType.repair,CheckedInventoryInout.BusinessTypePre.temp);
            }else if(WcabCellInventoryInout.BusinessType.recycle.name().equals(businessTypeChanged)){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId,WcabCheckedInventoryInout.BusinessType.recycle,WcabCheckedInventoryInout.BusinessTypePre.normal);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.recycle,checkedInventoryInout.getId());
//                checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId,CheckedInventoryInout.BusinessType.recycle,CheckedInventoryInout.BusinessTypePre.temp);
            }else*/
            if(WcabCellInventoryInout.BusinessType.losed.name().equals(businessTypeChanged)){
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutByCheckLose,WcabCellInventoryInout.BusinessTypeChecked.losed,null);
            }else{
//                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId, WcabCheckedInventoryInout.BusinessType.normal,WcabCheckedInventoryInout.BusinessTypePre.normal);
//                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypeChecked.normal,checkedInventoryInout.getId());
                 if (wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
                     inventoryList.forEach(e->{
                         WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=quantityMap2.get(e.getSkuCode());
                         if (wcabCellInventoryInoutDetailForm!=null){
                             e.setQuantityChecked(wcabCellInventoryInoutDetailForm.getQuantityChecked());
                             e.setWeightChecked(wcabCellInventoryInoutDetailForm.getWeightChecked());
                         }

                     });
                     this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypeChecked.normal,null);

                     inventoryList.forEach(e->{
                         WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm=quantityMap2.get(e.getSkuCode());
                         if (wcabCellInventoryInoutDetailForm!=null){
                             e.setQuantity(wcabCellInventoryInoutDetailForm.getQuantityChecked());
                             e.setQuantityChecked(wcabCellInventoryInoutDetailForm.getQuantityChecked());
                             e.setWeight(wcabCellInventoryInoutDetailForm.getWeightChecked());
                             e.setWeightChecked(wcabCellInventoryInoutDetailForm.getWeightChecked());
                             e.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCodeChecked());
                         }

                     });

//                     inventoryList.get(0).setSkuCode(inventoryList.get(0).getSkuCodeChecked());
//                     if (wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag()){
//                        inventoryList.get(0).setWeight(inventoryList.get(0).getWeightChecked());
//                     }
//
//                     if (wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag()){
//                         inventoryList.get(0).setQuantity(inventoryList.get(0).getQuantityChecked());
//                     }
                     this.inventoryIn4CheckQuantityWeight(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypePre.normal,null,null);
                     /**
                     if (wcabCellInventoryInoutForm.getWeightFlag()==null&&wcabCellInventoryInoutForm.getQuantityFlag()==null){
                         this.inventoryIn4CheckQuantityWeight(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypePre.normal,null,null);

                     }else if(wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag()
                             &&wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag()){
                         this.inventoryIn4CheckQuantity(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypePre.normal,null,null);
                         this.inventoryIn4CheckWeight(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypePre.normal,null,null);

                     }else if(wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag()){
                         this.inventoryIn4CheckWeight(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypePre.normal,null,null);
                     }else if(wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag()){
                         this.inventoryIn4CheckQuantity(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutByCheckSpec,WcabCellInventoryInout.BusinessTypePre.normal,null,null);
                     }*/


                 }else{
                     if ((wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag())&&
                             (wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag())&&
                             (
                                     (inventorySpaces.get(0).getWeight().doubleValue()>wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().doubleValue()&&inventorySpaces.get(0).getQuantity()>wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked())||
                                             (inventorySpaces.get(0).getWeight().doubleValue()<wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().doubleValue()&&inventorySpaces.get(0).getQuantity()<wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked())
                                     )
                             ){
                         if ((inventorySpaces.get(0).getWeight().doubleValue()>wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().doubleValue()&&inventorySpaces.get(0).getQuantity()>wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked())){
                             BigDecimal offsetWeight=inventorySpaces.get(0).getWeight().subtract(wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked());
                             Integer offsetQuantity=inventorySpaces.get(0).getQuantity()-wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked();
                             inventoryList.get(0).setWeight(offsetWeight);
                             inventoryList.get(0).setQuantity(offsetQuantity);
                             this.inventoryOut4CheckQuantityWeight(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalOutVirtualByCheckWeightQuantity,WcabCellInventoryInout.BusinessTypePre.normal,null,null);
                         }else if((inventorySpaces.get(0).getWeight().doubleValue()<wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().doubleValue()&&inventorySpaces.get(0).getQuantity()<wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked())){
                             BigDecimal offsetWeight=wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().subtract(inventorySpaces.get(0).getWeight());
                             Integer offsetQuantity=wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked()-inventorySpaces.get(0).getQuantity();
                             inventoryList.get(0).setWeight(offsetWeight);
                             inventoryList.get(0).setQuantity(offsetQuantity);
                             this.inventoryIn4CheckQuantityWeight(inventoryList,checkedResultId,businessType,WcabCellInventoryInout.OperationType.normalInVirtualByCheckWeightQuantity,WcabCellInventoryInout.BusinessTypePre.normal,null,null);
                         }


                     }else{
                         if (wcabCellInventoryInoutForm.getWeightFlag()!=null&&wcabCellInventoryInoutForm.getWeightFlag()){
                             if (inventorySpaces.get(0).getWeight().doubleValue()>wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().doubleValue()){
                                 BigDecimal offsetWeight=inventorySpaces.get(0).getWeight().subtract(wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked());
                                 inventoryList.get(0).setWeight(offsetWeight);
                                 this.inventoryOut4CheckOffsetWeight(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutVirtualByCheckWeight,WcabCellInventoryInout.BusinessTypeChecked.normal,null);
                             }else if(inventorySpaces.get(0).getWeight().doubleValue()<wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().doubleValue()){
                                 BigDecimal offsetWeight=wcabCellInventoryInoutForm.getDetailList().get(0).getWeightChecked().subtract(inventorySpaces.get(0).getWeight());
                                 inventoryList.get(0).setWeight(offsetWeight);
                                 this.inventoryIn4CheckOffsetWeight(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalInVirtualByCheckWeight,WcabCellInventoryInout.BusinessTypeChecked.normal,null);
                             }
                         }
                         if (wcabCellInventoryInoutForm.getQuantityFlag()!=null&&wcabCellInventoryInoutForm.getQuantityFlag()){
                             if (inventorySpaces.get(0).getQuantity()>wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked()){
                                 Integer offsetQuantity=inventorySpaces.get(0).getQuantity()-wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked();
                                 inventoryList.get(0).setQuantity(offsetQuantity);
                                 this.inventoryOut4CheckOffsetQuantity(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalOutVirtualByCheckWeight,WcabCellInventoryInout.BusinessTypeChecked.normal,null);
                             }else if(inventorySpaces.get(0).getQuantity()<wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked()){
                                 Integer offsetQuantity=wcabCellInventoryInoutForm.getDetailList().get(0).getQuantityChecked()-inventorySpaces.get(0).getQuantity();
                                 inventoryList.get(0).setQuantity(offsetQuantity);
                                 this.inventoryIn4CheckOffsetQuantity(inventoryList,WcabCellInventoryInout.BusinessType.normal,checkedResultId,WcabCellInventoryInout.OperationType.normalInVirtualByCheckWeight,WcabCellInventoryInout.BusinessTypeChecked.normal,null);
                             }
                         }
                     }

                 }
            }

        }else if(WcabCellInventoryInout.BusinessType.repair.name().equals(businessTypePre)){
//            this.inventoryOut4Check(inventoryList,StorageInventoryInout.BusinessType.repair,checkedResultId,StorageInventoryInout.BusinessDetailType.repairOutByCheck,StorageInventoryInout.BusinessTypeChecked.repair);
            /**
            if (WcabCellInventoryInout.BusinessType.normal.name().equals(businessTypeChanged)){
                WcabCellInventoryInout storageInventoryInout=this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.repair,checkedResultId,WcabCellInventoryInout.OperationType.repairOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.temp,null);
//                StorageInventoryInout storageInventoryInout=this.inventoryOut(list, StorageInventoryInout.BusinessType.temp,null,StorageInventoryInout.BusinessDetailType.tempOut);
                inventoryList.forEach(item->{
                    item.setCheckCabCode(item.getCabCode());
                    item.setCheckSpaceCode(item.getSpaceCode());
                    item.setCabCode(item.getTargetCabCode());
                    item.setSpaceCode(item.getTargetSpaceCode());
                });
                this.inventoryIn4Check(inventoryList,checkedResultId,WcabCellInventoryInout.BusinessType.normal,WcabCellInventoryInout.OperationType.normalInByCheck,WcabCellInventoryInout.BusinessTypePre.repair,storageInventoryInout.getId(),storageInventoryInout.getCheckInventoryInoutId());
            }else if(StorageInventoryInout.BusinessType.recycle.name().equals(businessTypeChanged)){

                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId,WcabCheckedInventoryInout.BusinessType.recycle,WcabCheckedInventoryInout.BusinessTypePre.repair);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.repair,checkedResultId,WcabCellInventoryInout.OperationType.repairOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.recycle,checkedInventoryInout.getId());
            }else*/
             if(StorageInventoryInout.BusinessType.repair.name().equals(businessTypeChanged)){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId,WcabCheckedInventoryInout.BusinessType.repair,WcabCheckedInventoryInout.BusinessTypePre.repair);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.repair,checkedResultId,WcabCellInventoryInout.OperationType.repairOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.repair,checkedInventoryInout.getId());
            }else if(StorageInventoryInout.BusinessType.losed.name().equals(businessTypeChanged)){
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.repair,checkedResultId,WcabCellInventoryInout.OperationType.repairOutByCheckLose,WcabCellInventoryInout.BusinessTypeChecked.losed,null);
            }else if(wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId, WcabCheckedInventoryInout.BusinessType.repair,WcabCheckedInventoryInout.BusinessTypePre.repair);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.repair,checkedResultId,WcabCellInventoryInout.OperationType.repairOutByCheckSpec,WcabCellInventoryInout.BusinessTypeChecked.repair,checkedInventoryInout.getId());
            }

        }else if(WcabCellInventoryInout.BusinessType.recycle.name().equals(businessTypePre)){
            /**
            if (WcabCellInventoryInout.BusinessType.normal.name().equals(businessTypeChanged)){
                WcabCellInventoryInout storageInventoryInout=this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.recycle,checkedResultId,WcabCellInventoryInout.OperationType.recycleOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.temp,null);
                inventoryList.forEach(item->{
                    item.setCheckCabCode(item.getCabCode());
                    item.setCheckSpaceCode(item.getSpaceCode());
                    item.setCabCode(item.getTargetCabCode());
                    item.setSpaceCode(item.getTargetSpaceCode());
                });
                this.inventoryIn4Check(inventoryList,checkedResultId,WcabCellInventoryInout.BusinessType.normal,WcabCellInventoryInout.OperationType.normalInByCheck,WcabCellInventoryInout.BusinessTypePre.recycle,storageInventoryInout.getId(),null);
            }else if(WcabCellInventoryInout.BusinessType.repair.name().equals(businessTypeChanged)){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId,WcabCheckedInventoryInout.BusinessType.repair,WcabCheckedInventoryInout.BusinessTypePre.recycle);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.recycle,checkedResultId,WcabCellInventoryInout.OperationType.recycleOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.repair,checkedInventoryInout.getId());
            }else*/
             if(WcabCellInventoryInout.BusinessType.recycle.name().equals(businessTypeChanged)){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId,WcabCheckedInventoryInout.BusinessType.recycle,WcabCheckedInventoryInout.BusinessTypePre.recycle);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.recycle,checkedResultId,WcabCellInventoryInout.OperationType.recycleOutByCheck,WcabCellInventoryInout.BusinessTypeChecked.recycle,checkedInventoryInout.getId());
            }else if(WcabCellInventoryInout.BusinessType.losed.name().equals(businessTypeChanged)){
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.recycle,checkedResultId,WcabCellInventoryInout.OperationType.recycleOutByCheckLose,WcabCellInventoryInout.BusinessTypeChecked.losed,null);
            }else if(wcabCellInventoryInoutForm.getSkuFlag()!=null&&wcabCellInventoryInoutForm.getSkuFlag()){
                WcabCheckedInventoryInout checkedInventoryInout=checkedInventoryInoutDetailService.inventoryIn(inventoryList,checkedResultId, WcabCheckedInventoryInout.BusinessType.recycle,WcabCheckedInventoryInout.BusinessTypePre.recycle);
                this.inventoryOut4Check(inventoryList,WcabCellInventoryInout.BusinessType.recycle,checkedResultId,WcabCellInventoryInout.OperationType.recycleOutByCheckSpec,WcabCellInventoryInout.BusinessTypeChecked.recycle,checkedInventoryInout.getId());
            }
        }

        return true;
    }

    @Override
    @Transactional
    public void inventoryIn4Check(List<WcabCellInventoryInoutDetailForm> list,Long checkedResultId, WcabCellInventoryInout.BusinessType businessType,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypePre businessTypePre,Long storageInventoryOutId,Long storageInventoryCheckId) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("IN-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessTypePre(businessTypePre);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setStorageInventoryOutId(storageInventoryOutId);
        wcabCellInventoryInout.setEnterpriseCode(JwtUtils.getEnterpriseCode());
        inventoryInoutService.save(wcabCellInventoryInout);
//        List<WcabCellInventoryInoutDetailForm> listIn=new ArrayList<>();
//        for (WcabCellInventoryInoutDetailForm detailForm:list){
//            UseInventoryInoutDetailForm cellInventoryInoutDetailForm=new UseInventoryInoutDetailForm();
//            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
//            listIn.add(cellInventoryInoutDetailForm);
//        }
        saveDetail(list,wcabCellInventoryInout.getId(),WcabUseInventoryInout.Type.in.name(),businessType,true);
//        useInventoryInoutDetailService.inventoryOut(list,wcabCellInventoryInout.getId());

    }

    @Override
    @Transactional
    public void inventoryIn4CheckQuantity(List<WcabCellInventoryInoutDetailForm> list,Long checkedResultId, WcabCellInventoryInout.BusinessType businessType,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypePre businessTypePre,Long storageInventoryOutId,Long storageInventoryCheckId) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("IN-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessTypePre(businessTypePre);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setStorageInventoryOutId(storageInventoryOutId);
        wcabCellInventoryInout.setEnterpriseCode(JwtUtils.getEnterpriseCode());
        inventoryInoutService.save(wcabCellInventoryInout);
//        List<WcabCellInventoryInoutDetailForm> listIn=new ArrayList<>();
//        for (WcabCellInventoryInoutDetailForm detailForm:list){
//            UseInventoryInoutDetailForm cellInventoryInoutDetailForm=new UseInventoryInoutDetailForm();
//            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
//            listIn.add(cellInventoryInoutDetailForm);
//        }
        saveDetail4CheckQuantity(list,wcabCellInventoryInout.getId(),WcabUseInventoryInout.Type.in.name(),businessType,true);
//        useInventoryInoutDetailService.inventoryOut(list,wcabCellInventoryInout.getId());

    }


    @Override
    @Transactional
    public void inventoryIn4CheckWeight(List<WcabCellInventoryInoutDetailForm> list,Long checkedResultId, WcabCellInventoryInout.BusinessType businessType,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypePre businessTypePre,Long storageInventoryOutId,Long storageInventoryCheckId) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("IN-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessTypePre(businessTypePre);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setStorageInventoryOutId(storageInventoryOutId);
        wcabCellInventoryInout.setEnterpriseCode(JwtUtils.getEnterpriseCode());
        inventoryInoutService.save(wcabCellInventoryInout);
//        List<WcabCellInventoryInoutDetailForm> listIn=new ArrayList<>();
//        for (WcabCellInventoryInoutDetailForm detailForm:list){
//            UseInventoryInoutDetailForm cellInventoryInoutDetailForm=new UseInventoryInoutDetailForm();
//            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
//            listIn.add(cellInventoryInoutDetailForm);
//        }
        saveDetail4CheckWeight(list,wcabCellInventoryInout.getId(),WcabUseInventoryInout.Type.in.name(),businessType,true);
//        useInventoryInoutDetailService.inventoryOut(list,wcabCellInventoryInout.getId());

    }


    @Override
    @Transactional
    public void inventoryIn4CheckQuantityWeight(List<WcabCellInventoryInoutDetailForm> list,Long checkedResultId, WcabCellInventoryInout.BusinessType businessType,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypePre businessTypePre,Long storageInventoryOutId,Long storageInventoryCheckId) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("IN-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessTypePre(businessTypePre);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setStorageInventoryOutId(storageInventoryOutId);
        wcabCellInventoryInout.setEnterpriseCode(JwtUtils.getEnterpriseCode());
        inventoryInoutService.save(wcabCellInventoryInout);
//        List<WcabCellInventoryInoutDetailForm> listIn=new ArrayList<>();
//        for (WcabCellInventoryInoutDetailForm detailForm:list){
//            UseInventoryInoutDetailForm cellInventoryInoutDetailForm=new UseInventoryInoutDetailForm();
//            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
//            listIn.add(cellInventoryInoutDetailForm);
//        }
        saveDetail4CheckQuantityWeight(list,wcabCellInventoryInout.getId(),WcabUseInventoryInout.Type.in.name(),businessType,true);
//        useInventoryInoutDetailService.inventoryOut(list,wcabCellInventoryInout.getId());

    }

    @Override
    @Transactional
    public void inventoryOut4CheckQuantityWeight(List<WcabCellInventoryInoutDetailForm> list,Long checkedResultId, WcabCellInventoryInout.BusinessType businessType,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypePre businessTypePre,Long storageInventoryOutId,Long storageInventoryCheckId) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OU-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setBusinessTypePre(businessTypePre);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setStorageInventoryOutId(storageInventoryOutId);
        wcabCellInventoryInout.setEnterpriseCode(JwtUtils.getEnterpriseCode());
        inventoryInoutService.save(wcabCellInventoryInout);
//        List<WcabCellInventoryInoutDetailForm> listIn=new ArrayList<>();
//        for (WcabCellInventoryInoutDetailForm detailForm:list){
//            UseInventoryInoutDetailForm cellInventoryInoutDetailForm=new UseInventoryInoutDetailForm();
//            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
//            listIn.add(cellInventoryInoutDetailForm);
//        }
        saveDetail4CheckQuantityWeight(list,wcabCellInventoryInout.getId(),WcabUseInventoryInout.Type.out.name(),businessType,true);
//        useInventoryInoutDetailService.inventoryOut(list,wcabCellInventoryInout.getId());

    }


    private Integer getCheckQuantity(List<WcabCellInventoryInoutDetailForm> list){
        Integer sum=0;
        for (WcabCellInventoryInoutDetailForm storageInventoryInoutDetailForm:list){
            sum+=storageInventoryInoutDetailForm.getQuantity();
        }
        return sum;
    }

    private BigDecimal getCheckWeight(List<WcabCellInventoryInoutDetailForm> list){
        BigDecimal sum=BigDecimal.valueOf(0);
        for (WcabCellInventoryInoutDetailForm storageInventoryInoutDetailForm:list){
            sum=sum.add(storageInventoryInoutDetailForm.getWeight());
        }
        return sum;
    }

    private Integer getQuantity(List<WcabCellInventorySpace> list){
        Integer sum=0;
        for (WcabCellInventorySpace wcabCellInventorySpace:list){
            sum+=wcabCellInventorySpace.getQuantity();
        }
        return sum;
    }


    @Override
    @Transactional
    public WcabCellInventoryInout inventoryOut4Check(List<WcabCellInventoryInoutDetailForm> list, WcabCellInventoryInout.BusinessType businessType,Long checkedResultId,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked,Long checkedInventoryInoutId) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OUC-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        wcabCellInventoryInout.setCheckTime(LocalDateTime.now());
        wcabCellInventoryInout.setCheckUserId(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCheckUserName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setIsCheck(true);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
//        wcabCellInventoryInout.setCompanyId(enterpriseId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setCheckInventoryInoutId(checkedInventoryInoutId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        wcabCellInventoryInout.setUseInventoryInoutId(usein)
//        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        inventoryInoutService.save(wcabCellInventoryInout);
        List<WcabUseInventoryInoutDetailForm> listOut=new ArrayList<>();
        for (WcabCellInventoryInoutDetailForm detailForm:list){
            WcabUseInventoryInoutDetailForm cellInventoryInoutDetailForm=new WcabUseInventoryInoutDetailForm();
            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
            listOut.add(cellInventoryInoutDetailForm);
        }
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
//            useInventoryInoutDetailService.inventoryIn4TempCabInventoryOut(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.virtualOutByCheck,null,StorageInventoryInout.BusinessDetailType.virtualOutByCheck,UseInventoryInout.Type.inByCheck);
        }
        Long cellInventoryCheckId=saveDetail4CheckOut(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.Type.out.name(),businessType,true,checkedResultId,businessDetailType,checkedInventoryInoutId,businessTypeChecked);
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
            for (int i=0;i<list.size();i++){
//                list.get(i).setSettingTime(listOut.get(i).getSettingTime());
//                list.get(i).setRemainingTime(listOut.get(i).getRemainingTime());
//                list.get(i).setLifeUnit(listOut.get(i).getLifeUnit());
            }
        }
//        wcabCellInventoryInout.setStorageInventoryCheckId(cellInventoryCheckId);
        return wcabCellInventoryInout;
    }


    @Override
    @Transactional
    public WcabCellInventoryInout inventoryOut4CheckOffsetQuantity(List<WcabCellInventoryInoutDetailForm> list, WcabCellInventoryInout.BusinessType businessType,Long checkedResultId,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked,Long checkedInventoryInoutId) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OUC-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        wcabCellInventoryInout.setCheckTime(LocalDateTime.now());
        wcabCellInventoryInout.setCheckUserId(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCheckUserName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setIsCheck(true);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
//        wcabCellInventoryInout.setCompanyId(enterpriseId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setCheckInventoryInoutId(checkedInventoryInoutId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        wcabCellInventoryInout.setUseInventoryInoutId(usein)
//        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        inventoryInoutService.save(wcabCellInventoryInout);
        List<WcabUseInventoryInoutDetailForm> listOut=new ArrayList<>();
        for (WcabCellInventoryInoutDetailForm detailForm:list){
            WcabUseInventoryInoutDetailForm cellInventoryInoutDetailForm=new WcabUseInventoryInoutDetailForm();
            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
            listOut.add(cellInventoryInoutDetailForm);
        }
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
//            useInventoryInoutDetailService.inventoryIn4TempCabInventoryOut(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.virtualOutByCheck,null,StorageInventoryInout.BusinessDetailType.virtualOutByCheck,UseInventoryInout.Type.inByCheck);
        }
        Long cellInventoryCheckId=saveDetail4OffsetQuantity(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.Type.out.name(),businessType,true,checkedResultId,businessDetailType,checkedInventoryInoutId,businessTypeChecked);
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
            for (int i=0;i<list.size();i++){
//                list.get(i).setSettingTime(listOut.get(i).getSettingTime());
//                list.get(i).setRemainingTime(listOut.get(i).getRemainingTime());
//                list.get(i).setLifeUnit(listOut.get(i).getLifeUnit());
            }
        }
//        wcabCellInventoryInout.setStorageInventoryCheckId(cellInventoryCheckId);
        return wcabCellInventoryInout;
    }

    @Override
    @Transactional
    public WcabCellInventoryInout inventoryOut4CheckOffsetWeight(List<WcabCellInventoryInoutDetailForm> list, WcabCellInventoryInout.BusinessType businessType,Long checkedResultId,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked,Long checkedInventoryInoutId) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("OUC-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.out);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        wcabCellInventoryInout.setCheckTime(LocalDateTime.now());
        wcabCellInventoryInout.setCheckUserId(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCheckUserName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setIsCheck(true);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
//        wcabCellInventoryInout.setCompanyId(enterpriseId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setCheckInventoryInoutId(checkedInventoryInoutId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        wcabCellInventoryInout.setUseInventoryInoutId(usein)
//        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        inventoryInoutService.save(wcabCellInventoryInout);
        List<WcabUseInventoryInoutDetailForm> listOut=new ArrayList<>();
        for (WcabCellInventoryInoutDetailForm detailForm:list){
            WcabUseInventoryInoutDetailForm cellInventoryInoutDetailForm=new WcabUseInventoryInoutDetailForm();
            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
            listOut.add(cellInventoryInoutDetailForm);
        }
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
//            useInventoryInoutDetailService.inventoryIn4TempCabInventoryOut(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.virtualOutByCheck,null,StorageInventoryInout.BusinessDetailType.virtualOutByCheck,UseInventoryInout.Type.inByCheck);
        }
        Long cellInventoryCheckId=saveDetail4OffsetWeight(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.Type.out.name(),businessType,true,checkedResultId,businessDetailType,checkedInventoryInoutId,businessTypeChecked);
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
            for (int i=0;i<list.size();i++){
//                list.get(i).setSettingTime(listOut.get(i).getSettingTime());
//                list.get(i).setRemainingTime(listOut.get(i).getRemainingTime());
//                list.get(i).setLifeUnit(listOut.get(i).getLifeUnit());
            }
        }
//        wcabCellInventoryInout.setStorageInventoryCheckId(cellInventoryCheckId);
        return wcabCellInventoryInout;
    }

    @Override
    @Transactional
    public WcabCellInventoryInout inventoryIn4CheckOffsetQuantity(List<WcabCellInventoryInoutDetailForm> list, WcabCellInventoryInout.BusinessType businessType,Long checkedResultId,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked,Long checkedInventoryInoutId) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("INC-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        wcabCellInventoryInout.setCheckTime(LocalDateTime.now());
        wcabCellInventoryInout.setCheckUserId(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCheckUserName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setIsCheck(true);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
//        wcabCellInventoryInout.setCompanyId(enterpriseId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setCheckInventoryInoutId(checkedInventoryInoutId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        wcabCellInventoryInout.setUseInventoryInoutId(usein)
//        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        inventoryInoutService.save(wcabCellInventoryInout);
        List<WcabUseInventoryInoutDetailForm> listOut=new ArrayList<>();
        for (WcabCellInventoryInoutDetailForm detailForm:list){
            WcabUseInventoryInoutDetailForm cellInventoryInoutDetailForm=new WcabUseInventoryInoutDetailForm();
            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
            listOut.add(cellInventoryInoutDetailForm);
        }
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
//            useInventoryInoutDetailService.inventoryIn4TempCabInventoryOut(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.virtualOutByCheck,null,StorageInventoryInout.BusinessDetailType.virtualOutByCheck,UseInventoryInout.Type.inByCheck);
        }
        Long cellInventoryCheckId=saveDetail4OffsetQuantity(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.Type.in.name(),businessType,true,checkedResultId,businessDetailType,checkedInventoryInoutId,businessTypeChecked);
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
            for (int i=0;i<list.size();i++){
//                list.get(i).setSettingTime(listOut.get(i).getSettingTime());
//                list.get(i).setRemainingTime(listOut.get(i).getRemainingTime());
//                list.get(i).setLifeUnit(listOut.get(i).getLifeUnit());
            }
        }
//        wcabCellInventoryInout.setStorageInventoryCheckId(cellInventoryCheckId);
        return wcabCellInventoryInout;
    }

    @Override
    @Transactional
    public WcabCellInventoryInout inventoryIn4CheckOffsetWeight(List<WcabCellInventoryInoutDetailForm> list, WcabCellInventoryInout.BusinessType businessType,Long checkedResultId,WcabCellInventoryInout.OperationType businessDetailType,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked,Long checkedInventoryInoutId) {
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        WcabCellInventoryInout wcabCellInventoryInout=new WcabCellInventoryInout();
        String code=dynamicService.getDayNextCode("wcab_cell_inventory_inout");
        String day=simpleDateFormat.format(new Date());
        wcabCellInventoryInout.setCode("INC-"+day+"-"+code);
        wcabCellInventoryInout.setDay(Integer.valueOf(day));
        wcabCellInventoryInout.setNo(Integer.valueOf(code));
        wcabCellInventoryInout.setCreator(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCreatorName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setInoutType(WcabCellInventoryInout.InoutType.in);
        wcabCellInventoryInout.setBusinessType(businessType);
        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        wcabCellInventoryInout.setCheckTime(LocalDateTime.now());
        wcabCellInventoryInout.setCheckUserId(JwtUtils.getLoginUserId());
        wcabCellInventoryInout.setCheckUserName(JwtUtils.getLoginUsername());
        wcabCellInventoryInout.setStatus(WcabCellInventoryInout.Status.checked);
        wcabCellInventoryInout.setIsCheck(true);
        wcabCellInventoryInout.setCheckedResultId(checkedResultId);
//        wcabCellInventoryInout.setCompanyId(enterpriseId);
        wcabCellInventoryInout.setOperationType(businessDetailType);
        wcabCellInventoryInout.setRemark(businessDetailType.getDesc());
        wcabCellInventoryInout.setCheckInventoryInoutId(checkedInventoryInoutId);
        wcabCellInventoryInout.setEnterpriseCode(enterpriseCode);

//        wcabCellInventoryInout.setUseInventoryInoutId(usein)
//        wcabCellInventoryInout.setBusinessTypeChecked(businessTypeChecked);
        inventoryInoutService.save(wcabCellInventoryInout);
        List<WcabUseInventoryInoutDetailForm> listOut=new ArrayList<>();
        for (WcabCellInventoryInoutDetailForm detailForm:list){
            WcabUseInventoryInoutDetailForm cellInventoryInoutDetailForm=new WcabUseInventoryInoutDetailForm();
            BeanUtils.copyProperties(detailForm,cellInventoryInoutDetailForm);
            listOut.add(cellInventoryInoutDetailForm);
        }
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
//            useInventoryInoutDetailService.inventoryIn4TempCabInventoryOut(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.BusinessType.virtualOutByCheck,null,StorageInventoryInout.BusinessDetailType.virtualOutByCheck,UseInventoryInout.Type.inByCheck);
        }
        Long cellInventoryCheckId=saveDetail4OffsetWeight(listOut,wcabCellInventoryInout.getId(),UseInventoryInout.Type.in.name(),businessType,true,checkedResultId,businessDetailType,checkedInventoryInoutId,businessTypeChecked);
        if (StorageInventoryInout.BusinessTypeChecked.temp.equals(businessTypeChecked)){
            for (int i=0;i<list.size();i++){
//                list.get(i).setSettingTime(listOut.get(i).getSettingTime());
//                list.get(i).setRemainingTime(listOut.get(i).getRemainingTime());
//                list.get(i).setLifeUnit(listOut.get(i).getLifeUnit());
            }
        }
//        wcabCellInventoryInout.setStorageInventoryCheckId(cellInventoryCheckId);
        return wcabCellInventoryInout;
    }



    @Transactional
    Long saveCheckedResult(WcabCellInventoryInoutForm wcabCellInventoryInoutForm,String businessTypePre,Integer quantityCheck,BigDecimal weightCheck){
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        String creatorName=JwtUtils.getLoginUsername();
        Long creator=JwtUtils.getLoginUserId();
        Long resultId=saveCheckedResult(wcabCellInventoryInoutForm.getDetailList().get(0),wcabCellInventoryInoutForm.getBusinessType(),businessTypePre,quantityCheck,weightCheck,wcabCellInventoryInoutForm.getFlag(),wcabCellInventoryInoutForm.getCategoryId());
        for (WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm:wcabCellInventoryInoutForm.getDetailList()){
            WcabCheckedResultDetail checkedResultDetail=new WcabCheckedResultDetail();
            checkedResultDetail.setResultId(resultId);
            checkedResultDetail.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
            checkedResultDetail.setCabId(wcabCellInventoryInoutDetailForm.getCabId());
            checkedResultDetail.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
            checkedResultDetail.setSkuId(wcabCellInventoryInoutDetailForm.getSkuId());
            checkedResultDetail.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());
            checkedResultDetail.setSpaceId(wcabCellInventoryInoutDetailForm.getSpaceId());
            checkedResultDetail.setQuantity(wcabCellInventoryInoutDetailForm.getQuantityPre());
            checkedResultDetail.setWeight(wcabCellInventoryInoutDetailForm.getWeightPre());
            checkedResultDetail.setQuantityChecked(wcabCellInventoryInoutDetailForm.getQuantityChecked());
            checkedResultDetail.setWeightChecked(wcabCellInventoryInoutDetailForm.getWeightChecked());
            checkedResultDetail.setBusinessType(WcabCheckedResultDetail.BusinessType.jsonCreator(businessTypePre));
            checkedResultDetail.setBusinessTypeChecked(WcabCheckedResultDetail.BusinessTypeChecked.jsonCreator(wcabCellInventoryInoutForm.getBusinessType().name()));
            checkedResultDetail.setSkuCodeChecked(wcabCellInventoryInoutDetailForm.getSkuCodeChecked());
            log.info("{},{},{}",WcabCellInventoryInout.BusinessTypeChecked.losed.name(),wcabCellInventoryInoutForm.getBusinessType().name(),WcabCellInventoryInout.BusinessTypeChecked.losed.name().equals(wcabCellInventoryInoutForm.getBusinessType().name()));
            if (WcabCellInventoryInout.BusinessTypeChecked.losed.name().equals(wcabCellInventoryInoutForm.getBusinessType().name())){
                checkedResultDetail.setQuantityChecked(0);
                checkedResultDetail.setWeightChecked(BigDecimal.valueOf(0l));
            }
            checkedResultDetail.setCreator(creator);
            checkedResultDetail.setCreatorName(creatorName);
            checkedResultDetail.setEnterpriseCode(enterpriseCode);
            if (WcabCellInventoryInout.BusinessType.normal.equals(wcabCellInventoryInoutForm.getBusinessType().name())){
                checkedResultDetail.setOperationType(WcabCheckedResultDetail.OperationType.in);
            }else{
                checkedResultDetail.setOperationType(WcabCheckedResultDetail.OperationType.out);
            }
            checkedResultDetail.setRemark(wcabCellInventoryInoutForm.getRemark());
            checkedResultDetailService.save(checkedResultDetail);

        }
        return resultId;
    }

    Long saveCheckedResult(WcabCellInventoryInoutDetailForm wcabCellInventoryInoutDetailForm,WcabCellInventoryInout.BusinessType businessType,String businessTypePre,Integer quantityCheck,BigDecimal weightCheck,Boolean correctFlag,Long causeCategoryId){
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        List<WcabCellInventoryCheck> storageInventoryChecks=storageInventoryCheckService.list(new QueryWrapper<WcabCellInventoryCheck>().eq("cab_code",wcabCellInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabCellInventoryInoutDetailForm.getSpaceCode()).eq("status",StorageInventoryCheck.Status.created.getValue()));
        WcabCheckedResult wcabCheckedResult=new WcabCheckedResult();
        if (storageInventoryChecks.size()>0){
            wcabCheckedResult.setCheckId(storageInventoryChecks.get(0).getId());
        }
        wcabCheckedResult.setCabCode(wcabCellInventoryInoutDetailForm.getCabCode());
//        wcabCheckedResult.setCabId(wcabCellInventoryInoutDetailForm.getCabId());
        wcabCheckedResult.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//        wcabCheckedResult.setSkuId(wcabCellInventoryInoutDetailForm.getSkuId());
        wcabCheckedResult.setSpaceCode(wcabCellInventoryInoutDetailForm.getSpaceCode());
//        wcabCheckedResult.setSpaceId(wcabCellInventoryInoutDetailForm.getSpaceId());
        wcabCheckedResult.setQuantity(wcabCellInventoryInoutDetailForm.getQuantity());
        wcabCheckedResult.setStatus(WcabCheckedResult.Status.checked);
        wcabCheckedResult.setBusinessType(WcabCheckedResult.BusinessType.jsonCreator(businessType.name()));
        wcabCheckedResult.setBusinessTypePre(WcabCheckedResult.BusinessTypePre.jsonCreator(businessTypePre));
        wcabCheckedResult.setCreator(JwtUtils.getLoginUserId());
        wcabCheckedResult.setCreatorName(JwtUtils.getLoginUsername());
        wcabCheckedResult.setQuantity(quantityCheck);
        wcabCheckedResult.setWeight(weightCheck);
//        wcabCheckedResult.setIsCorrect(businessType.name().equals(businessTypePre));
//        wcabCheckedResult.setIsCorrect(wcabCellInventoryInoutDetailForm.getIsCorrect());
//        wcabCheckedResult.setIsCorrect(correctFlag);
        wcabCheckedResult.setIsCorrect(wcabCellInventoryInoutDetailForm.getIsCorrect());
        wcabCheckedResult.setSkuCorrect(wcabCellInventoryInoutDetailForm.getSkuCorrect());
        wcabCheckedResult.setBusinessTypeCorrect(businessType.name().equals(businessTypePre));
        wcabCheckedResult.setWeightCorrect(wcabCellInventoryInoutDetailForm.getWeightCorrect());
        wcabCheckedResult.setQuantityCorrect(wcabCellInventoryInoutDetailForm.getQuantityCorrect());
        wcabCheckedResult.setCategoryId(causeCategoryId);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("wcab_checked_result");
        String day=simpleDateFormat.format(new Date());
        wcabCheckedResult.setCode("RE-"+day+"-"+code);
        wcabCheckedResult.setDay(Integer.valueOf(day));
        wcabCheckedResult.setNo(Integer.valueOf(code));
        wcabCheckedResult.setCompanyId(enterpriseId);
        wcabCheckedResult.setEnterpriseCode(enterpriseCode);
        wcabCheckedResult.setRemark(wcabCellInventoryInoutDetailForm.getRemark());
        checkedResultService.save(wcabCheckedResult);
        return wcabCheckedResult.getId();
    }

    @Transactional
    Long saveDetail4CheckOut(List<WcabUseInventoryInoutDetailForm> list, Long storageInventoryInoutId, String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,boolean isCheck,Long checkResultId,WcabCellInventoryInout.OperationType businessDetailType,Long checkInventoryInoutId,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked){
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long cellInventoryCheckId=null;
        if (CollectionUtils.isNotEmpty(list)){
            for (WcabUseInventoryInoutDetailForm useInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(useInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
//                wcabCellInventoryInoutDetail.setCompanyId(enterpriseId);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
                wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.jsonCreator(inOutTypeName));
                wcabCellInventoryInoutDetail.setBusinessType(WcabCellInventoryInoutDetail.BusinessType.jsonCreator(businessType.name()));
                wcabCellInventoryInoutDetail.setInoutId(storageInventoryInoutId);
                if (isCheck){
                    wcabCellInventoryInoutDetail.setBusinessTypeChecked(WcabCellInventoryInoutDetail.BusinessTypeChecked.jsonCreator(businessTypeChecked.name()));
//                    wcabCellInventoryInoutDetail.setQuantityChecked(useInventoryInoutDetailForm.getQuantity());
                    if (StorageInventoryInout.BusinessType.losed.equals(businessType)){
                        wcabCellInventoryInoutDetail.setQuantityChecked(0);
                        wcabCellInventoryInoutDetail.setWeightChecked(BigDecimal.valueOf(0));
                    }
                }

                if (isCheck&&
                        (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                    List<WcabCellInventoryCheck> wcabCellInventoryChecks=storageInventoryCheckService.list(new QueryWrapper<WcabCellInventoryCheck>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).eq("status",StorageInventoryCheck.Status.created.getValue()));
                    if (wcabCellInventoryChecks.size()>0){
                        cellInventoryCheckId=wcabCellInventoryChecks.get(0).getId();
                    }
                }

//                List<StorageInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<StorageInventorySpace>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",useInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
//                List<StorageInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<StorageInventorySpace>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",useInventoryInoutDetailForm.getSkuCode()));
                List<CabinetCargoLane> cabinetCargoLanes= cargoLaneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("code",useInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoLanes.size()==0){
                    throw new BusinessException("未找到库位");
                }
//                if (CabinetCargoSpace.SpaceNature.personal.equals(cabinetCargoLanes.get(0).getSpaceNature())){
//                    if (!creator.equals(cabinetCargoLanes.get(0).getPrincipal())){
//                        throw new BusinessException("柜子编号["+cabinetCargoLanes.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
//                    }
//                }
//                List<WcabCellInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()));

                List<WcabCellInventorySpace> spaceSkuList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",useInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellInventoryProduct> skuInventoryList=inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",useInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()).eq("enterprise_code",enterpriseCode));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList=storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("sku_code",useInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));

                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(useInventoryInoutDetailForm.getSkuCode());
                ProductDetail.InventoryUnitType inventoryUnitType=null;
                if (productDetailVO!=null){
                    inventoryUnitType=productDetailVO.getInventoryUnitType();
                }

                if (UseInventoryInout.Type.out.name().equals(inOutTypeName)){
//                    if (spaceSkuList.size()==0){
//                        throw new BusinessException("未找到柜子编号["+useInventoryInoutDetailForm.getCabCode()+"],货道编号["+useInventoryInoutDetailForm.getSpaceCode()+"]上的物料");
//                    }else{
//                        useInventoryInoutDetailForm.setSkuCode(spaceSkuList.get(0).getSkuCode());
                        Integer quantity=useInventoryInoutDetailForm.getQuantity();
                        BigDecimal weight=useInventoryInoutDetailForm.getWeight();
                        if(ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                            if (quantity<=0){
                                throw new BusinessException("取出数量参数异常");
                            }
                        }else{
                            if (weight.doubleValue()<=0){
                                throw new BusinessException("取出重量参数异常");
                            }
                        }

                        if (quantity>0){
                            quantity=-1*quantity;
                        }
                        if (weight.doubleValue()>0){
                            weight=weight.multiply(BigDecimal.valueOf(-1));
                        }
                        if(spaceSkuList.size()>0){
                            Integer currentQuantity=spaceSkuList.get(0).getQuantity();
                            BigDecimal currentWeight=spaceSkuList.get(0).getWeight();
                            Integer newQuantity=currentQuantity+quantity;
                            BigDecimal newWeight=currentWeight.add(weight);
                            if(ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (newQuantity<0){
//                                throw new BusinessException("柜子编号["+useInventoryInoutDetailForm.getCabCode()+"],货道编号["+useInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                                }
                            }else{
                                if(newWeight.doubleValue()<0){
//                                throw new BusinessException("柜子编号["+useInventoryInoutDetailForm.getCabCode()+"],货道编号["+useInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                                }

                            }

                            WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                            wcabCellInventorySpace.setId(spaceSkuList.get(0).getId());
                            wcabCellInventorySpace.setQuantity(newQuantity);
                            wcabCellInventorySpace.setWeight(newWeight);
                            if(ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (newQuantity<=0){
                                    inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                                }else{
                                    inventorySpaceService.updateById(wcabCellInventorySpace);
                                }
                            }else{
                                if (newWeight.doubleValue()<=0){
                                    inventorySpaceService.removeById(spaceSkuList.get(0).getId());
                                }else{
                                    inventorySpaceService.updateById(wcabCellInventorySpace);
                                }
                            }
                            wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                            wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                            wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                            wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        }



                        if(skuSingleInventoryList.size()>0){
                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(useInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(useInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                            wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                            if(ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (wcabCellSingleInventoryProduct.getQuantity()<=0){
                                    storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                                }else{
                                    storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                                }
                            }else{
                                if (wcabCellSingleInventoryProduct.getWeight().doubleValue()<=0){
                                    storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                                }else{
                                    storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                                }
                            }

                        }

                        if(skuInventoryList.size()>0){
                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(useInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                            wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                            if(ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
                                if (wcabCellInventoryProduct.getQuantity()<=0){
                                    inventoryProductService.removeById(skuInventoryList.get(0).getId());
                                }else{
                                    inventoryProductService.updateById(wcabCellInventoryProduct);
                                }
                            }else{
                                if (wcabCellInventoryProduct.getWeight().doubleValue()<=0){
                                    inventoryProductService.removeById(skuInventoryList.get(0).getId());
                                }else{
                                    inventoryProductService.updateById(wcabCellInventoryProduct);
                                }
                            }

                        }


                        wcabCellInventoryInoutDetail.setQuantity(quantity);

                        wcabCellInventoryInoutDetail.setWeight(weight);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
//                    }
                    if (isCheck&&
                            (StorageInventoryInout.BusinessType.repair.equals(businessType)||StorageInventoryInout.BusinessType.recycle.equals(businessType))){
                        log.info("useInventoryInoutDetailForm check param:{}", JSON.toJSONString(useInventoryInoutDetailForm));
                        List<Integer> businessTypes=new ArrayList<>();
                        businessTypes.add(WcabCellInventorySpace.BusinessType.repair.getValue());
                        businessTypes.add(WcabCellInventorySpace.BusinessType.recycle.getValue());
                        int count=inventorySpaceService.count(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).in("business_type",businessTypes));
                        if (count==0){
                            WcabCellInventoryCheck wcabCellInventoryCheck=new WcabCellInventoryCheck();
                            wcabCellInventoryCheck.setStatus(WcabCellInventoryCheck.Status.checked);
                            storageInventoryCheckService.update(wcabCellInventoryCheck,new QueryWrapper<WcabCellInventoryCheck>().eq("cab_code",useInventoryInoutDetailForm.getCabCode()).eq("space_code",useInventoryInoutDetailForm.getSpaceCode()).eq("status",StorageInventoryCheck.Status.created.getValue()));
                        }
                    }


                }else if(UseInventoryInout.Type.in.name().equals(inOutTypeName)){

                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交存放刀具数据");
        }
        return cellInventoryCheckId;
    }


    @Transactional
    Long saveDetail4OffsetQuantity(List<WcabUseInventoryInoutDetailForm> list, Long storageInventoryInoutId, String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,boolean isCheck,Long checkResultId,WcabCellInventoryInout.OperationType businessDetailType,Long checkInventoryInoutId,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked){
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long cellInventoryCheckId=null;
        if (CollectionUtils.isNotEmpty(list)){
            for (WcabUseInventoryInoutDetailForm wcabUseInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabUseInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
//                wcabCellInventoryInoutDetail.setCompanyId(enterpriseId);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
                wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.jsonCreator(inOutTypeName));
                wcabCellInventoryInoutDetail.setBusinessType(WcabCellInventoryInoutDetail.BusinessType.jsonCreator(businessType.name()));
                wcabCellInventoryInoutDetail.setInoutId(storageInventoryInoutId);
                if (isCheck){
                    wcabCellInventoryInoutDetail.setBusinessTypeChecked(WcabCellInventoryInoutDetail.BusinessTypeChecked.jsonCreator(businessTypeChecked.name()));
//                    wcabCellInventoryInoutDetail.setQuantityChecked(wcabUseInventoryInoutDetailForm.getQuantity());
                    if (StorageInventoryInout.BusinessType.losed.equals(businessType)){
                        wcabCellInventoryInoutDetail.setQuantityChecked(0);
                        wcabCellInventoryInoutDetail.setWeightChecked(BigDecimal.valueOf(0));
                    }
                }

                if (isCheck&&
                        (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                    List<WcabCellInventoryCheck> wcabCellInventoryChecks=storageInventoryCheckService.list(new QueryWrapper<WcabCellInventoryCheck>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("status",StorageInventoryCheck.Status.created.getValue()));
                    if (wcabCellInventoryChecks.size()>0){
                        cellInventoryCheckId=wcabCellInventoryChecks.get(0).getId();
                    }
                }

//                List<StorageInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<StorageInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
//                List<StorageInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<StorageInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()));
                List<CabinetCargoLane> cabinetCargoLanes= cargoLaneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("code",wcabUseInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoLanes.size()==0){
                    throw new BusinessException("未找到库位");
                }
//                if (CabinetCargoSpace.SpaceNature.personal.equals(cabinetCargoLanes.get(0).getSpaceNature())){
//                    if (!creator.equals(cabinetCargoLanes.get(0).getPrincipal())){
//                        throw new BusinessException("柜子编号["+cabinetCargoLanes.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
//                    }
//                }
//                List<WcabCellInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()));

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellInventoryProduct> skuInventoryList=inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()).eq("enterprise_code",enterpriseCode));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList=storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));

                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());

                if (UseInventoryInout.Type.out.name().equals(inOutTypeName)){
//                    if (spaceInventoryList.size()==0){
//                        throw new BusinessException("未找到柜子编号["+wcabUseInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabUseInventoryInoutDetailForm.getSpaceCode()+"]上的物料");
//                    }else{
//                        wcabUseInventoryInoutDetailForm.setSkuCode(spaceInventoryList.get(0).getSkuCode());
                        Integer quantity=wcabUseInventoryInoutDetailForm.getQuantity();
                        BigDecimal weight=wcabUseInventoryInoutDetailForm.getWeight();
                        if (quantity<=0){
                            throw new BusinessException("取出数量参数异常");
                        }

                        if (quantity>0){
                            quantity=-1*quantity;
                        }
                        if (spaceInventoryList.size()>0){
                            Integer currentQuantity=spaceInventoryList.get(0).getQuantity();

                            Integer newQuantity=currentQuantity+quantity;

                            if ((productDetailVO!=null&&ProductDetail.InventoryUnitType.quantity.equals(productDetailVO.getInventoryUnitType()))&&newQuantity<0){
//                            throw new BusinessException("柜子编号["+wcabUseInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabUseInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                            }

                            WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                            wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                            wcabCellInventorySpace.setQuantity(newQuantity);
                            if (newQuantity<=0){
                                inventorySpaceService.removeById(spaceInventoryList.get(0).getId());
                            }else{
                                inventorySpaceService.updateById(wcabCellInventorySpace);
                            }
                            wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                            wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        }



                        if(skuSingleInventoryList.size()>0){
                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(wcabUseInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);

                            storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                        }

                        if(skuInventoryList.size()>0){
                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);

                            inventoryProductService.updateById(wcabCellInventoryProduct);
                        }


                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeightAfter(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
//                    }



                }else if(UseInventoryInout.Type.in.name().equals(inOutTypeName)){

                    Integer quantity=wcabUseInventoryInoutDetailForm.getQuantity();
                    if (quantity<=0){
                        throw new BusinessException("上货数量参数异常");
                    }
                    BigDecimal weight=wcabUseInventoryInoutDetailForm.getWeight();
                    if (weight.doubleValue()<0){
                        throw new BusinessException("上货重量参数异常");
                    }

                        Integer currentQuantity=spaceInventoryList.get(0).getQuantity();
                        Integer newQuantity=currentQuantity+quantity;


                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                        wcabCellInventorySpace.setQuantity(newQuantity);
//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(currentQuantity);
                        wcabCellInventoryInoutDetail.setQuantityAfter(newQuantity);
                        wcabCellInventoryInoutDetail.setQuantity(quantity);
                        wcabCellInventoryInoutDetail.setWeightCurrent(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeightAfter(spaceInventoryList.get(0).getWeight());
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);


                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabUseInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setQuantity(skuSingleInventoryList.get(0).getQuantity()+quantity);
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);



                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setQuantity(skuInventoryList.get(0).getQuantity()+quantity);
                        inventoryProductService.updateById(wcabCellInventoryProduct);


                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交存放刀具数据");
        }
        return cellInventoryCheckId;
    }

    @Transactional
    Long saveDetail4OffsetWeight(List<WcabUseInventoryInoutDetailForm> list, Long storageInventoryInoutId, String inOutTypeName,WcabCellInventoryInout.BusinessType businessType,boolean isCheck,Long checkResultId,WcabCellInventoryInout.OperationType businessDetailType,Long checkInventoryInoutId,WcabCellInventoryInout.BusinessTypeChecked businessTypeChecked){
        Long enterpriseId=JwtUtils.getEnterpriseId();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        Long creator=JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        Long cellInventoryCheckId=null;
        if (CollectionUtils.isNotEmpty(list)){
            for (WcabUseInventoryInoutDetailForm wcabUseInventoryInoutDetailForm:list){
                WcabCellInventoryInoutDetail wcabCellInventoryInoutDetail=new WcabCellInventoryInoutDetail();
                BeanUtils.copyProperties(wcabUseInventoryInoutDetailForm,wcabCellInventoryInoutDetail);
                wcabCellInventoryInoutDetail.setEnterpriseCode(enterpriseCode);
//                wcabCellInventoryInoutDetail.setCompanyId(enterpriseId);
                wcabCellInventoryInoutDetail.setId(null);
                wcabCellInventoryInoutDetail.setCreator(creator);
                wcabCellInventoryInoutDetail.setCreatorName(creatorName);
                wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.jsonCreator(inOutTypeName));
                wcabCellInventoryInoutDetail.setBusinessType(WcabCellInventoryInoutDetail.BusinessType.jsonCreator(businessType.name()));
                wcabCellInventoryInoutDetail.setInoutId(storageInventoryInoutId);
                if (isCheck){
                    wcabCellInventoryInoutDetail.setBusinessTypeChecked(WcabCellInventoryInoutDetail.BusinessTypeChecked.jsonCreator(businessTypeChecked.name()));
//                    wcabCellInventoryInoutDetail.setQuantityChecked(wcabUseInventoryInoutDetailForm.getQuantity());
                    if (StorageInventoryInout.BusinessType.losed.equals(businessType)){
                        wcabCellInventoryInoutDetail.setQuantityChecked(0);
                        wcabCellInventoryInoutDetail.setWeightChecked(BigDecimal.valueOf(0));
                    }
                }

                if (isCheck&&
                        (WcabCellInventoryInout.BusinessType.repair.equals(businessType)||WcabCellInventoryInout.BusinessType.recycle.equals(businessType))){
                    List<WcabCellInventoryCheck> wcabCellInventoryChecks=storageInventoryCheckService.list(new QueryWrapper<WcabCellInventoryCheck>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("status",StorageInventoryCheck.Status.created.getValue()));
                    if (wcabCellInventoryChecks.size()>0){
                        cellInventoryCheckId=wcabCellInventoryChecks.get(0).getId();
                    }
                }

//                List<StorageInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<StorageInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));
//                List<StorageInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<StorageInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()));
                List<CabinetCargoLane> cabinetCargoLanes= cargoLaneService.list(new QueryWrapper<CabinetCargoLane>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("code",wcabUseInventoryInoutDetailForm.getSpaceCode()));
                if (cabinetCargoLanes.size()==0){
                    throw new BusinessException("未找到库位");
                }
//                if (CabinetCargoSpace.SpaceNature.personal.equals(cabinetCargoLanes.get(0).getSpaceNature())){
//                    if (!creator.equals(cabinetCargoLanes.get(0).getPrincipal())){
//                        throw new BusinessException("柜子编号["+cabinetCargoLanes.get(0).getCode()+"]为用户的专人存贮,其他用户不能打开");
//                    }
//                }
//                List<WcabCellInventorySpace> spaceInventoryList=inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()));

                List<WcabCellInventorySpace> spaceInventoryList= inventorySpaceService.list(new QueryWrapper<WcabCellInventorySpace>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("space_code",wcabUseInventoryInoutDetailForm.getSpaceCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()));
                List<WcabCellInventoryProduct> skuInventoryList=inventoryProductService.list(new QueryWrapper<WcabCellInventoryProduct>().eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()).eq("enterprise_code",enterpriseCode));
                List<WcabCellSingleInventoryProduct> skuSingleInventoryList=storageSingleInventoryProductService.list(new QueryWrapper<WcabCellSingleInventoryProduct>().eq("cab_code",wcabUseInventoryInoutDetailForm.getCabCode()).eq("sku_code",wcabUseInventoryInoutDetailForm.getSkuCode()).eq("business_type",businessType.getValue()));

                ProductDetailVO productDetailVO=productDetailService.getProductDetailBySkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());

                if (UseInventoryInout.Type.out.name().equals(inOutTypeName)){
//                    if (spaceInventoryList.size()==0){
//                        throw new BusinessException("未找到柜子编号["+wcabUseInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabUseInventoryInoutDetailForm.getSpaceCode()+"]上的物料");
//                    }else{
//                        wcabUseInventoryInoutDetailForm.setSkuCode(spaceInventoryList.get(0).getSkuCode());

                        BigDecimal weight=wcabUseInventoryInoutDetailForm.getWeight();

                        if (weight.doubleValue()<=0){
                            throw new BusinessException("取出重量参数异常");
                        }

                        if (weight.doubleValue()>0){
                            weight=weight.multiply(BigDecimal.valueOf(-1));
                        }

                        if(spaceInventoryList.size()>0){
                            BigDecimal currentWeight=spaceInventoryList.get(0).getWeight();

                            BigDecimal newWeight=currentWeight.add(weight);

                            if ((productDetailVO!=null&&ProductDetail.InventoryUnitType.weight.equals(productDetailVO.getInventoryUnitType()))&&newWeight.doubleValue()<0){
//                            throw new BusinessException("柜子编号["+wcabUseInventoryInoutDetailForm.getCabCode()+"],货道编号["+wcabUseInventoryInoutDetailForm.getSpaceCode()+"]上的产品库存不足");
                            }
                            WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                            wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());

                            wcabCellInventorySpace.setWeight(newWeight);
                            if (newWeight.doubleValue()<=0){
                                inventorySpaceService.removeById(spaceInventoryList.get(0).getId());
                            }else{
                                inventorySpaceService.updateById(wcabCellInventorySpace);
                            }
                            wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                            wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        }


                        if(skuSingleInventoryList.size()>0){
                            WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                            wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                            wcabCellSingleInventoryProduct.setCabCode(wcabUseInventoryInoutDetailForm.getCabCode());
                            wcabCellSingleInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                            wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                            if (wcabCellSingleInventoryProduct.getWeight().doubleValue()<=0){
                                storageSingleInventoryProductService.removeById(skuSingleInventoryList.get(0).getId());
                            }else{
                                storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);
                            }
                        }

                        if(skuInventoryList.size()>0){
                            WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                            wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                            wcabCellInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                            wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                            if (wcabCellInventoryProduct.getWeight().doubleValue()<=0){
                                inventoryProductService.removeById(skuInventoryList.get(0).getId());
                            }else{
                                inventoryProductService.updateById(wcabCellInventoryProduct);
                            }
                        }

                        wcabCellInventoryInoutDetail.setQuantityCurrent(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantityAfter(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantity(spaceInventoryList.get(0).getQuantity());

                        wcabCellInventoryInoutDetail.setWeight(weight);
                        baseMapper.insert(wcabCellInventoryInoutDetail);
//                    }



                }else if(UseInventoryInout.Type.in.name().equals(inOutTypeName)){


                    BigDecimal weight=wcabUseInventoryInoutDetailForm.getWeight();
                    if (weight.doubleValue()<0){
                        throw new BusinessException("上货重量参数异常");
                    }



                        BigDecimal currentWeight=spaceInventoryList.get(0).getWeight();
                        BigDecimal newWeight=currentWeight.add(weight);

                        WcabCellInventorySpace wcabCellInventorySpace=new WcabCellInventorySpace();
                        wcabCellInventorySpace.setId(spaceInventoryList.get(0).getId());
                        wcabCellInventorySpace.setWeight(newWeight);
//                        if (spaceInventoryList.get(0).getQuantity()==0){
//                            wcabCellInventorySpace.setSkuCode(wcabCellInventoryInoutDetailForm.getSkuCode());
//                        }


                        inventorySpaceService.updateById(wcabCellInventorySpace);


                        wcabCellInventoryInoutDetail.setQuantityCurrent(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantityAfter(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setQuantity(spaceInventoryList.get(0).getQuantity());
                        wcabCellInventoryInoutDetail.setWeightCurrent(currentWeight);
                        wcabCellInventoryInoutDetail.setWeightAfter(newWeight);
                        wcabCellInventoryInoutDetail.setWeight(weight);
                        wcabCellInventoryInoutDetail.setOperationType(WcabCellInventoryInoutDetail.OperationType.in);
                        baseMapper.insert(wcabCellInventoryInoutDetail);

                        WcabCellSingleInventoryProduct wcabCellSingleInventoryProduct=new WcabCellSingleInventoryProduct();
                        wcabCellSingleInventoryProduct.setId(skuSingleInventoryList.get(0).getId());
                        wcabCellSingleInventoryProduct.setCabCode(wcabUseInventoryInoutDetailForm.getCabCode());
                        wcabCellSingleInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                        wcabCellSingleInventoryProduct.setWeight(skuSingleInventoryList.get(0).getWeight().add(weight));
                        storageSingleInventoryProductService.updateById(wcabCellSingleInventoryProduct);

                        WcabCellInventoryProduct wcabCellInventoryProduct=new WcabCellInventoryProduct();
                        wcabCellInventoryProduct.setId(skuInventoryList.get(0).getId());
                        wcabCellInventoryProduct.setSkuCode(wcabUseInventoryInoutDetailForm.getSkuCode());
                        wcabCellInventoryProduct.setWeight(skuInventoryList.get(0).getWeight().add(weight));
                        inventoryProductService.updateById(wcabCellInventoryProduct);

                }else{
                    throw new BusinessException("出入库类型参数异常");
                }

            }
        }else{
            throw new BusinessException("未提交存放刀具数据");
        }
        return cellInventoryCheckId;
    }

    public List<CabinetCargoLaneVO> getSpaces(String cabCodes, String skuCode, WcabCellInventoryInout.BusinessType businessType, List<Integer> spaceTypes, BigDecimal weight) {

        SystemRuleInfo systemRuleInfo = systemRuleInfoService.getOne(new QueryWrapper<SystemRuleInfo>());
        Boolean spaceUserShareFlag = systemRuleInfo.getSpaceUserShareFlag();
        Boolean spaceSkuShareFlag = systemRuleInfo.getSpaceSkuShareFlag();
//        Boolean spaceTempMoreFlag = systemRuleInfo.getSpaceTempMoreFlag();
        Boolean spaceTempUserShareFlag = systemRuleInfo.getSpaceTempUserShareFlag();
        Boolean spaceTempSkuShareFlag = systemRuleInfo.getSpaceTempSkuShareFlag();
        if (spaceTempSkuShareFlag || spaceTempUserShareFlag) {
//            spaceTempMoreFlag = true;
        }

        Long creator = JwtUtils.getLoginUserId();
//        CabinetCargoLane.SpaceType spaceType = null;
//        if (WcabCellInventoryInout.BusinessType.recycle.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.recycle;
//        } else if (WcabCellInventoryInout.BusinessType.repair.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.repair;
//        } else if (WcabCellInventoryInout.BusinessType.normal.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.normal;
//        } else {
//            throw new BusinessException("库位性质参数异常");
//        }
        List<ProductDetail> productDetails = productDetailService.list(new QueryWrapper<ProductDetail>().eq("sku_code", skuCode));
        ProductDetail.InventoryUnitType inventoryUnitType=null;
        if (productDetails.size()>0){
            inventoryUnitType=productDetails.get(0).getInventoryUnitType();
        }
        Long userId=JwtUtils.getLoginUserId();
        List<CabinetCargoLaneVO> cabinetCargoLaneVOS = cargoLaneService.getWcabSpaceByCabCode(cabCodes,spaceTypes);
        List<CabinetCargoLaneVO> resultList=new ArrayList<>();
        for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
            if (cabinetCargoLaneVO.getIsDelete()){
                continue;
            }
            if (cabinetCargoLaneVO.getBusinessType()!=null&&!cabinetCargoLaneVO.getBusinessType().name().equals(businessType.name())){
                continue;
            }
            if (cabinetCargoLaneVO.getWeightInventory().doubleValue() >= cabinetCargoLaneVO.getWeightMax().doubleValue()||
                    (cabinetCargoLaneVO.getWeightInventory().add(weight).doubleValue()>cabinetCargoLaneVO.getWeightMax().doubleValue())) {
                continue;
            }

            if (StringUtils.isNotBlank(cabinetCargoLaneVO.getSkuCode())&&!skuCode.equals(cabinetCargoLaneVO.getSkuCode())){
                continue;
            }
            if (cabinetCargoLaneVO.getSpaceProductType()!=null&&cabinetCargoLaneVO.getSpaceProductType() != 0&&
                    productDetails.size()>0&&productDetails.get(0).getProductTypeId()!=null&&productDetails.get(0).getProductTypeId()!=0&&
                    !cabinetCargoLaneVO.getSpaceProductType().equals(productDetails.get(0).getProductTypeId())
            ) {
                continue;

            }
            if (
//                    !spaceTempSkuShareFlag&&
                    StringUtils.isNotBlank(cabinetCargoLaneVO.getSkuCodeCurrent())
                    &&!skuCode.equals(cabinetCargoLaneVO.getSkuCodeCurrent())){
                continue;
            }
            resultList.add(cabinetCargoLaneVO);
        }
        return resultList;
    }

    public List<CabinetCargoLaneVO> getOptimalSpaces(String cabCodes, String skuCode, WcabCellInventoryInout.BusinessType businessType, List<Integer> spaceTypes, BigDecimal weight,Integer quantity) {

        SystemRuleInfo systemRuleInfo = systemRuleInfoService.getOne(new QueryWrapper<SystemRuleInfo>());
        Boolean spaceUserShareFlag = systemRuleInfo.getSpaceUserShareFlag();
        Boolean spaceSkuShareFlag = systemRuleInfo.getSpaceSkuShareFlag();
//        Boolean spaceTempMoreFlag = systemRuleInfo.getSpaceTempMoreFlag();
        Boolean spaceTempUserShareFlag = systemRuleInfo.getSpaceTempUserShareFlag();
        Boolean spaceTempSkuShareFlag = systemRuleInfo.getSpaceTempSkuShareFlag();
        if (spaceTempSkuShareFlag || spaceTempUserShareFlag) {
//            spaceTempMoreFlag = true;
        }

        Long creator = JwtUtils.getLoginUserId();
//        CabinetCargoLane.SpaceType spaceType = null;
//        if (WcabCellInventoryInout.BusinessType.recycle.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.recycle;
//        } else if (WcabCellInventoryInout.BusinessType.repair.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.repair;
//        } else if (WcabCellInventoryInout.BusinessType.normal.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.normal;
//        } else {
//            throw new BusinessException("库位性质参数异常");
//        }
        List<ProductDetail> productDetails = productDetailService.list(new QueryWrapper<ProductDetail>().eq("sku_code", skuCode));
        ProductDetail.InventoryUnitType inventoryUnitType=null;
        if (productDetails.size()>0){
            inventoryUnitType=productDetails.get(0).getInventoryUnitType();
        }
        Long userId=JwtUtils.getLoginUserId();
        List<CabinetCargoLaneVO> cabinetCargoLaneVOS = cargoLaneService.getWcabOptimalSpaceByCabCode(cabCodes,spaceTypes,skuCode, GlobalConstants.DESC);
        List<CabinetCargoLaneVO> resultList=new ArrayList<>();
        boolean bool=false;
        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
            for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                if (cabinetCargoLaneVO.getQuantity()>=quantity){
                    resultList.add(cabinetCargoLaneVO);
                    bool=true;
                    break;
                }
            }
            if (!bool){
                Integer quantitySum=0;
                for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                    resultList.add(cabinetCargoLaneVO);
                    quantitySum+=cabinetCargoLaneVO.getQuantityInventory();
                    if (quantitySum>=quantity){
                        bool=true;
                        break;
                    }
                }
            }
        }else{
            for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                if (cabinetCargoLaneVO.getWeightInventory().doubleValue()>=weight.doubleValue()){
                    resultList.add(cabinetCargoLaneVO);
                    bool=true;
                    break;
                }
            }
            if (!bool){
                BigDecimal weightSum=BigDecimal.valueOf(0);
                for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                    resultList.add(cabinetCargoLaneVO);
                    weightSum=weightSum.add(cabinetCargoLaneVO.getWeightInventory());
                    if (weightSum.doubleValue()>=weight.doubleValue()){
                        bool=true;
                        break;
                    }
                }
            }
        }

        return resultList;
    }

    public List<CabinetCargoLaneVO> getReturnOptimalSpaces(String cabCodes, String skuCode, WcabCellInventoryInout.BusinessType businessType, List<Integer> spaceTypes, BigDecimal weight,Integer quantity) {

        SystemRuleInfo systemRuleInfo = systemRuleInfoService.getOne(new QueryWrapper<SystemRuleInfo>());
        Boolean spaceUserShareFlag = systemRuleInfo.getSpaceUserShareFlag();
        Boolean spaceSkuShareFlag = systemRuleInfo.getSpaceSkuShareFlag();
//        Boolean spaceTempMoreFlag = systemRuleInfo.getSpaceTempMoreFlag();
        Boolean spaceTempUserShareFlag = systemRuleInfo.getSpaceTempUserShareFlag();
        Boolean spaceTempSkuShareFlag = systemRuleInfo.getSpaceTempSkuShareFlag();
        if (spaceTempSkuShareFlag || spaceTempUserShareFlag) {
//            spaceTempMoreFlag = true;
        }

        Long creator = JwtUtils.getLoginUserId();
//        CabinetCargoLane.SpaceType spaceType = null;
//        if (WcabCellInventoryInout.BusinessType.recycle.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.recycle;
//        } else if (WcabCellInventoryInout.BusinessType.repair.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.repair;
//        } else if (WcabCellInventoryInout.BusinessType.normal.equals(businessType)) {
//            spaceType = CabinetCargoLane.SpaceType.normal;
//        } else {
//            throw new BusinessException("库位性质参数异常");
//        }
        List<ProductDetail> productDetails = productDetailService.list(new QueryWrapper<ProductDetail>().eq("sku_code", skuCode));
        ProductDetail.InventoryUnitType inventoryUnitType=null;
        if (productDetails.size()>0){
            inventoryUnitType=productDetails.get(0).getInventoryUnitType();
        }
        Long userId=JwtUtils.getLoginUserId();
        List<CabinetCargoLaneVO> cabinetCargoLaneVOS = cargoLaneService.getWcabOptimalSpaceByCabCode(cabCodes,spaceTypes,skuCode,GlobalConstants.ASC);
        List<CabinetCargoLaneVO> resultList=new ArrayList<>();
        boolean bool=false;
        if (ProductDetail.InventoryUnitType.quantity.equals(inventoryUnitType)){
            for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                Integer surplusWeight=cabinetCargoLaneVO.getQuantityMax()-cabinetCargoLaneVO.getQuantityInventory();
                if (surplusWeight>=quantity){
                    resultList.add(cabinetCargoLaneVO);
                    bool=true;
                    break;
                }
            }
            if (!bool){
                List<CabinetCargoLaneVO> emptySpaces=cargoLaneService.getWcabEmptySpaces(cabCodes);
                if (emptySpaces.size()>0){
                    Integer weightSum=0;
                    for (CabinetCargoLaneVO cabinetCargoLaneVO : emptySpaces) {
                        if (cabinetCargoLaneVO.getIsDelete()){
                            continue;
                        }
                        Integer surplusWeight=cabinetCargoLaneVO.getQuantityMax();
                        resultList.add(cabinetCargoLaneVO);
                        weightSum=weightSum+surplusWeight;
                        if (weightSum>=quantity){
                            bool=true;
                            break;
                        }
                    }
                }
            }


            if (!bool){
                Integer weightSum=0;
                for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                    Integer surplusWeight=cabinetCargoLaneVO.getQuantityMax()-cabinetCargoLaneVO.getQuantityInventory();
                    if (surplusWeight.doubleValue()>0){
                        resultList.add(cabinetCargoLaneVO);
                        weightSum=weightSum+surplusWeight;
                    }
                    if (weightSum>=quantity){
                        bool=true;
                        break;
                    }
                }
            }
        }else{
            for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                BigDecimal surplusWeight=cabinetCargoLaneVO.getWeightMax().subtract(cabinetCargoLaneVO.getWeightInventory());
                if (surplusWeight.doubleValue()>=weight.doubleValue()){
                    resultList.add(cabinetCargoLaneVO);
                    bool=true;
                    break;
                }
            }
            if (!bool){
                List<CabinetCargoLaneVO> emptySpaces=cargoLaneService.getWcabEmptySpaces(cabCodes);
                if (emptySpaces.size()>0){
                    BigDecimal weightSum=BigDecimal.valueOf(0);
                    for (CabinetCargoLaneVO cabinetCargoLaneVO : emptySpaces) {
                        if (cabinetCargoLaneVO.getIsDelete()){
                            continue;
                        }
                        BigDecimal surplusWeight=cabinetCargoLaneVO.getWeightMax();
                        resultList.add(cabinetCargoLaneVO);
                        weightSum=weightSum.add(surplusWeight);
                        if (weightSum.doubleValue()>=weight.doubleValue()){
                            bool=true;
                            break;
                        }
                    }
                }
            }


            if (!bool){
                BigDecimal weightSum=BigDecimal.valueOf(0);
                for (CabinetCargoLaneVO cabinetCargoLaneVO : cabinetCargoLaneVOS) {
                    BigDecimal surplusWeight=cabinetCargoLaneVO.getWeightMax().subtract(cabinetCargoLaneVO.getWeightInventory());
                    if (surplusWeight.doubleValue()>0){
                        resultList.add(cabinetCargoLaneVO);
                        weightSum=weightSum.add(surplusWeight);
                    }
                    if (weightSum.doubleValue()>=weight.doubleValue()){
                        bool=true;
                        break;
                    }
                }
            }
        }

        if (bool){
            return resultList;
        }else{
            return new ArrayList<CabinetCargoLaneVO>();
        }

    }

    @Override
    public JsonResult verifyPermissions(String skuCode,String cabCode){
        //根据cabCode查询主柜
        List<Cabinet> cabinetCodes = cabinetService.list(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getCode, cabCode));
        List<Cabinet> cabinets = cabinetService.list(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getCabCode, cabinetCodes.get(0).getBelongCabinet()));
        //查找该柜子上的启用状态的权限组
        List<WauthorityManage> manageList = authorityManageService.list(new LambdaQueryWrapper<WauthorityManage>()
                .eq(WauthorityManage::getCabinetId, cabinets.get(0).getId()).eq(WauthorityManage::getStatus,0l));
        //获得当前登录人所在的权限组
        List<WauthorityMember> authorityMemberList = authorityMemberService.list(new LambdaQueryWrapper<WauthorityMember>().eq(WauthorityMember::getUserId, JwtUtils.getLoginUserId()));
        //要领取的物料
        ProductDetailVO productDetailVO = productDetailService.getProductDetailBySkuCode(skuCode);
        if(manageList.size()>0){
            //柜子有权限组
            if(authorityMemberList.size()>0){
                //当前登录人有权限组
                //查看当前登录人所在的权限组是否在该柜子上
                Integer a =0;
                Long id = null;
                loop : for(WauthorityManage authorityManageItem:manageList){
                    for(WauthorityMember wauthorityMember:authorityMemberList){
                        if (authorityManageItem.getId()==wauthorityMember.getAuthorityManageId()){
                            a++;
                            id=authorityManageItem.getId();
                            break loop;
                        }
                    }
                }
                if (a>0){
                    //当前登录人所在的权限组在该柜子上
                    WauthorityManage  authorityManageItem = authorityManageService.getById(id);
                    boolean flag = false;
                    if(authorityManageItem.getRestrictWay().equals(WauthorityManage.RestrictWay.category)){
                       //权限组的领取范围
                        String[] rangeArray = authorityManageItem.getMaterialId().split(",");
                        flag=Arrays.asList(rangeArray).contains(productDetailVO.getProductCategoryId().toString());
                        if(flag){
                            //领取时 权限组是限制方式按分类  现在领取的这个物料是该分类下新添加的  之前新建该权限组时没有该物料   权限组物料表要加上
                            List<WauthorityMateriel> authorityMaterielList =  authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id",authorityManageItem.getId()));
                            Integer b = 0;
                            for (WauthorityMateriel authorityMateriel:authorityMaterielList){
                                if (authorityMateriel.getSkuCode().equals(skuCode)){
                                    b++;
                                }
                            }
                            if (b<1){
                                WauthorityMateriel authorityMateriel = new WauthorityMateriel();
                                CopyOptions copyOptions = CopyOptions.create()
                                        .setEditable(WauthorityMateriel.class)
                                        .setIgnoreError(true)
                                        .setIgnoreNullValue(true);
                                BeanUtil.copyProperties(authorityMaterielList.get(0), authorityMateriel, copyOptions);
                                authorityMateriel.setSkuCode(skuCode);
                                authorityMaterielService.save(authorityMateriel);
                            }
                            return JsonResult.success();
                        }else{
                            //这个权限组不包含要领取的物料，看这个物料是否在这柜子别的权限组上，在不能领，不在，能领
                            QueryWrapper<WauthorityManage> queryWrapper = new QueryWrapper<WauthorityManage>();
                            queryWrapper.ne("id",id);
                            queryWrapper.eq("status",0l);
                            queryWrapper.eq("cabinet_id",cabinets.get(0).getId());
                            List<WauthorityManage> list = authorityManageService.list(queryWrapper);
                            for(WauthorityManage wauthorityManage:list){
                                List<WauthorityMateriel> authorityMaterielList =  authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id",wauthorityManage.getId()));
                                for (WauthorityMateriel wauthorityMateriel:authorityMaterielList){
                                    if(wauthorityMateriel.getSkuCode().equals(skuCode)){
                                        return JsonResult.fail("您没有领取该物料的权限,请联系管理员");
                                    }
                                }
                            }
                            return JsonResult.success();
                        }
                    }else if(authorityManageItem.getRestrictWay().equals(WauthorityManage.RestrictWay.materiel)) {
                        //限制方式按物料
                        Integer c = 0;
                        //权限组可领取的sku
                        List<WauthorityMateriel> authorityMaterielList = authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id", authorityManageItem.getId()));
                        for (WauthorityMateriel authorityMateriel : authorityMaterielList) {
                            if (skuCode.equals(authorityMateriel.getSkuCode())) {
                                c++;
                            }
                        }
                        if (c == 0) {
                            //这个权限组不包含要领取的物料，看这个物料是否在别的权限组上，在不能领，不在，能领
                            QueryWrapper<WauthorityManage> queryWrapper = new QueryWrapper<WauthorityManage>();
                            queryWrapper.ne("id",id);
                            queryWrapper.eq("status",0l);
                            queryWrapper.eq("cabinet_id",cabinets.get(0).getId());
                            List<WauthorityManage> list = authorityManageService.list(queryWrapper);
                            for(WauthorityManage wauthorityManage:list){
                                List<WauthorityMateriel> wauthorityMaterielList =  authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id",wauthorityManage.getId()));
                                for (WauthorityMateriel wauthorityMateriel:wauthorityMaterielList){
                                    if(wauthorityMateriel.getSkuCode().equals(skuCode)){
                                        return JsonResult.fail("您没有领取该物料的权限,请联系管理员");
                                    }
                                }
                            }
                            return JsonResult.success();
                        }else{
                            return JsonResult.success();
                        }
                    }
                }else{
                    //当前登录人所在的权限组不在该柜子上
                    //没有加入柜子上权限组的物料可以领  加入了的不能领
                    for (WauthorityManage wauthorityManage:manageList){
                        if(wauthorityManage.getRestrictWay().equals(WauthorityManage.RestrictWay.category)){
                            boolean flag = false;
                            String[] rangeArray = wauthorityManage.getMaterialId().split(",");
                            flag=Arrays.asList(rangeArray).contains(productDetailVO.getProductCategoryId().toString());
                            if(flag){
                                return JsonResult.fail("您没有领取该物料的权限,请联系管理员");
                            }
                        }else if(wauthorityManage.getRestrictWay().equals(WauthorityManage.RestrictWay.materiel)) {
                            Integer c = 0;
                            //权限组可领取的sku
                            List<WauthorityMateriel> authorityMaterielList = authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id", wauthorityManage.getId()));
                            for (WauthorityMateriel authorityMateriel : authorityMaterielList) {
                                if (skuCode.equals(authorityMateriel.getSkuCode())) {
                                    c++;
                                }
                            }
                            if (c != 0){
                                return JsonResult.fail("您没有领取该物料的权限,请联系管理员");
                            }
                        }
                    }
                }
            }else{
                //当前登录人没有权限组
                //没有加入柜子上权限组的物料可以领  加入了的不能领
                for (WauthorityManage wauthorityManage:manageList){
                    if(wauthorityManage.getRestrictWay().equals(WauthorityManage.RestrictWay.category)){
                        boolean flag = false;
                        String[] rangeArray = wauthorityManage.getMaterialId().split(",");
                        flag=Arrays.asList(rangeArray).contains(productDetailVO.getProductCategoryId().toString());
                        if(flag){
                            return JsonResult.fail("您没有领取该物料的权限,请联系管理员");
                        }
                    }else if(wauthorityManage.getRestrictWay().equals(WauthorityManage.RestrictWay.materiel)) {
                        Integer c = 0;
                        //权限组可领取的sku
                        List<WauthorityMateriel> authorityMaterielList = authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id", wauthorityManage.getId()));
                        for (WauthorityMateriel authorityMateriel : authorityMaterielList) {
                            if (skuCode.equals(authorityMateriel.getSkuCode())) {
                                c++;
                            }
                        }
                        if (c != 0){
                            return JsonResult.fail("您没有领取该物料的权限,请联系管理员");
                        }
                    }
                }
            }
        }else{
            //柜子没有权限组 随便领
            return JsonResult.success();
        }
        return JsonResult.success();
    }

   /* @Override
    public JsonResult verifyPermissions(String skuCode,String cabCode) {
            //根据cabCode查询主柜
            List<Cabinet> cabinetCodes = cabinetService.list(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getCode, cabCode));
            List<Cabinet> cabinets = cabinetService.list(new LambdaQueryWrapper<Cabinet>().eq(Cabinet::getCabCode, cabinetCodes.get(0).getBelongCabinet()));
            //查找柜子的权限组
            List<WauthorityManage> manageList = authorityManageService.list(new LambdaQueryWrapper<WauthorityManage>()
                    .eq(WauthorityManage::getCabinetId, cabinets.get(0).getId()));
            Boolean isExist = false;
            Boolean isDisabled = false;
            //要领取的物料
            ProductDetailVO productDetailVO = productDetailService.getProductDetailBySkuCode(skuCode);
            if(manageList.size()>0){
                for(WauthorityManage authorityManageItem:manageList){
                    //状态正常的 启用状态的
                    if(authorityManageItem.getStatus() == 0){
                        isDisabled = true;
                        //权限组是否被禁用
                        if(isDisabled){
                            //该权限组的所有成员
                            List<WauthorityMember> authorityMemberList = authorityMemberService.list(new LambdaQueryWrapper<WauthorityMember>().eq(WauthorityMember::getAuthorityManageId, authorityManageItem.getId()));
                            //该人是否在权限组
                            List<WauthorityMember> memberList = authorityMemberList.stream().filter(s -> s.getUserId().equals(JwtUtils.getLoginUserId())).collect(Collectors.toList());
                            if(memberList.size()>0){//当前用户在该权限组
                                isExist = true;
                            }

                            //判断当前领取人是否有权限
                            if(isExist){
                                boolean flag = true;
                                //限制方式按分类
                                if(authorityManageItem.getRestrictWay().equals(WauthorityManage.RestrictWay.category)){
                                    //权限组的领取范围
                                    String[] rangeArray = authorityManageItem.getMaterialId().split(",");
                                        if (productDetailVO.getProductCategoryId()==null){
                                            flag=false;
                                        }else{
                                            flag=Arrays.asList(rangeArray).contains(productDetailVO.getProductCategoryId().toString());
                                        }
                                        if(!flag){
                                            return JsonResult.fail("该产品不在可领物料分类范围内！！！");
                                        }
                                        if(flag){
                                            //领取时 权限组是限制方式按分类  现在领取的这个物料是该分类下新添加的  之前新建该权限组时没有该物料   权限组物料表要加上
                                            List<WauthorityMateriel> authorityMaterielList =  authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id",authorityManageItem.getId()));
                                            Integer a = 0;
                                            for (WauthorityMateriel authorityMateriel:authorityMaterielList){
                                                if (authorityMateriel.getSkuCode().equals(skuCode)){
                                                    a++;
                                                }
                                            }
                                            if (a<1){
                                                WauthorityMateriel authorityMateriel = new WauthorityMateriel();
                                                CopyOptions copyOptions = CopyOptions.create()
                                                        .setEditable(WauthorityMateriel.class)
                                                        .setIgnoreError(true)
                                                        .setIgnoreNullValue(true);
                                                BeanUtil.copyProperties(authorityMaterielList.get(0), authorityMateriel, copyOptions);
                                                authorityMateriel.setSkuCode(skuCode);
                                                authorityMaterielService.save(authorityMateriel);
                                            }
                                        }
                                    }else if(authorityManageItem.getRestrictWay().equals(WauthorityManage.RestrictWay.materiel)) {
                                    //限制方式按物料
                                    Integer a = 0;
                                    //权限组可领取的sku
                                    List<WauthorityMateriel> authorityMaterielList = authorityMaterielService.list(new QueryWrapper<WauthorityMateriel>().eq("manage_id", authorityManageItem.getId()));
                                    for (WauthorityMateriel authorityMateriel : authorityMaterielList) {
                                        if (skuCode.equals(authorityMateriel.getSkuCode())) {
                                            a++;
                                        }
                                    }
                                    if (a == 0) {
                                        return JsonResult.fail("该产品不在可领物料范围内！！！");
                                    }
                                }
                            }
                        }
                    }
                }
            }else {
                return JsonResult.success();
            }
        return JsonResult.success();
    }*/

}
